django-models

Django queryset attach or annotate related object field

旧时模样 提交于 2020-07-04 10:51:14
问题 It is needed to attach to queryset results related object field. Models: class User(models.Model): name = models.CharField(max_length=50) friends = models.ManyToManyField('self', through='Membership', blank=True, null=True, symmetrical=False) class Membership(models.Model): status = models.CharField(choices=SOME_CHOICES, max_length=50) from_user = models.ForeignKey(User, related_name="member_from") to_user = models.ForeignKey(User, related_name="member_to") I can do this: >>> User.objects.all

Flush just an app not the whole project

做~自己de王妃 提交于 2020-07-04 07:36:25
问题 python manage.py flush removes data from the entire project. I would like to be able to do python manage.py flush agivenapp How can I do this? 回答1: sqlclear management command can be helpful... Usage: ./manage.py sqlclear [options] <appname appname ...> Prints the DROP TABLE SQL statements for the given app name(s). for the postgresql you can do: ./manage.py sqlclear myapp | psql dbname UPDATE for apps with migrations and Django 1.7+: python manage.py migrate <app> zero 回答2: You can do this

What is the best practice to validate a Django DateField at the model level as particular day of the week?

匆匆过客 提交于 2020-07-03 04:26:11
问题 I have a model: class MyModel(models.Model): user = models.ForeignKey(User) week = models.DateField() My app is being built around "weeks ending on a Saturday", so I would like the week field to only accept days that are a Saturday. I'd like this validation to happen in the Model rather than in a Form , as I'm going to have data input without a Form in certain circumstances. I understand that overriding the model's save() method to add a full_clean() is probably the way to go, but I'm

What is the best practice to validate a Django DateField at the model level as particular day of the week?

大憨熊 提交于 2020-07-03 04:26:09
问题 I have a model: class MyModel(models.Model): user = models.ForeignKey(User) week = models.DateField() My app is being built around "weeks ending on a Saturday", so I would like the week field to only accept days that are a Saturday. I'd like this validation to happen in the Model rather than in a Form , as I'm going to have data input without a Form in certain circumstances. I understand that overriding the model's save() method to add a full_clean() is probably the way to go, but I'm

How to use Django models outside of Django?

你。 提交于 2020-07-02 18:46:11
问题 Following this guide, I am able to use models outside of Django with the following file structure by calling python main.py . ├── data │ ├── __init__.py │ ├── migrations │ │ └── __init__.py │ └── models.py ├── main.py ├── manage.py └── settings.py where main.py looks like this: import os, django os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings') django.setup() from data.models import Foo, Bar #... print(Foo.objects.all()) #this works fine What I want to do is turn this into a

How to use Django models outside of Django?

自作多情 提交于 2020-07-02 18:45:30
问题 Following this guide, I am able to use models outside of Django with the following file structure by calling python main.py . ├── data │ ├── __init__.py │ ├── migrations │ │ └── __init__.py │ └── models.py ├── main.py ├── manage.py └── settings.py where main.py looks like this: import os, django os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings') django.setup() from data.models import Foo, Bar #... print(Foo.objects.all()) #this works fine What I want to do is turn this into a

Django: How to filter on inner join based on properties of second table?

喜你入骨 提交于 2020-06-29 11:33:08
问题 My question is simple: in a Django app, I have a table Users and a table StatusUpdates . In the StatusUpdates table, I have a column user which is a foreign key pointing back to Users . How can I do a search expressing something like: users.filter(latest_status_update.text__contains='Hello') Edit: Please excuse my lack of clarity. The query that I would like to make is something like "Give me all the users whose latest status update contains the text 'hello'". In Django code, I would do the

Django 3.0 images: Unable to display images in template

大兔子大兔子 提交于 2020-06-29 05:04:35
问题 I have model named Book in models.py file. And based on this model, a view has been created to display images as products. Which renders books(products) on shop.html template. Problem is that i am unable to get their cover images which are saved across each publishers id who is seller of those books. This is code of shop.html (in which i am trying to display image). <div class="container mt-4"> <div class="row"> {% for b in books|slice:":10" %} <div class="col-lg-2 col-md-3 col-sm-4"> <div

Django how to call a method from a custom field given a model instance?

半城伤御伤魂 提交于 2020-06-28 10:22:11
问题 I have the following model: class CustomField(models.CharField): def foo(self): return 'foo' class Test(models.Model): col1 = models.CharField(max_length=45) col2 = CustomField(max_length=45) How can I call the foo method from CustomField , if I'm given an instance of Test ? For example: >>> t = Test.objects.create(col1='bar', col2='blah') >>> t.col2 'blah' >>> t.col2.foo() # 'str' object has not attribute 'foo' 'foo' This, of course, throws: 'str' object has not attribute 'foo' because

Select specific fields in Django get_object_or_404

不问归期 提交于 2020-06-27 13:08:44
问题 I have a model in Django with too many fields. Ex: class MyModel(models.Model): param_1 = models.CharField(max_length=100) ... param_25 = models.CharField(max_length=100) Now I need to get the detail view based on an id. I have seen may methods like, obj = MyModel.objects.get(pk=5) obj = MyModel.objects.filter(pk=5)[0] obj = get_object_or_404(MyModel, pk=1) The last method suits the best as I can provide a 404 error without any code change. But I need only param_1 and param_2. Hence I need a