django-models

Difference between auto_now and auto_now_add

核能气质少年 提交于 2021-01-20 23:41:09
问题 What I understood in Django models field's attributes is auto_now - updates the value of field to current time and date every time the Model.save() is called. auto_now_add - updates the value with the time and date of creation of record. My question is what if a filed in model contains both the auto_now and auto_now_add set to True? What happens in that case? 回答1: auto_now takes precedence (obviously, because it updates field each time, while auto_now_add updates on creation only). Here is

Django: Customize the User model's return field

岁酱吖の 提交于 2021-01-20 19:22:49
问题 The default return field of Django's User model is 'username'. However, my application needs it to be 'first_name'. Currently, I just forked Django and made this change to the User model: def __str__(self): return self.first_name It works fine, but I wonder if there are other SIMPLE ways of doing this without forking Django? 回答1: There is a way to inject a new method on your User class. from django.contrib.auth.models import User def get_first_name(self): return self.first_name User.add_to

Django: Customize the User model's return field

喜欢而已 提交于 2021-01-20 19:21:36
问题 The default return field of Django's User model is 'username'. However, my application needs it to be 'first_name'. Currently, I just forked Django and made this change to the User model: def __str__(self): return self.first_name It works fine, but I wonder if there are other SIMPLE ways of doing this without forking Django? 回答1: There is a way to inject a new method on your User class. from django.contrib.auth.models import User def get_first_name(self): return self.first_name User.add_to

Multiple many-to-many relations to the same model in Django

杀马特。学长 韩版系。学妹 提交于 2021-01-20 17:08:13
问题 Given the following model with two many-to-many relations: class Child(models.Model): name = models.CharField(max_length=80) class Foo(models.Model): bar = models.ManyToManyField(Child) baz = models.ManyToManyField(Child) This gives the error: accounts.foo: Accessor for m2m field 'bar' clashes with related m2m field 'Child.foo_set'. Add a related_name argument to the definition for 'bar'. accounts.foo: Accessor for m2m field 'baz' clashes with related m2m field 'Child.foo_set'. Add a related

whats the difference between Django models and forms?

怎甘沉沦 提交于 2021-01-20 14:49:23
问题 I am new to Django and can't understand the models and forms. Can any one suggest me the differences and tutorials related to them. 回答1: Basically, a model encapsulates information about something (i.e., it models it), and is stored in the database. For example, we could model a person: from django import models class Person(models.Model): name = models.CharField(max_length=100) age = models.PositiveIntegerField() height = models.FloatField() weight = models.FloatField() Whenever a model

whats the difference between Django models and forms?

笑着哭i 提交于 2021-01-20 14:48:37
问题 I am new to Django and can't understand the models and forms. Can any one suggest me the differences and tutorials related to them. 回答1: Basically, a model encapsulates information about something (i.e., it models it), and is stored in the database. For example, we could model a person: from django import models class Person(models.Model): name = models.CharField(max_length=100) age = models.PositiveIntegerField() height = models.FloatField() weight = models.FloatField() Whenever a model

NoReverseMatch at / Reverse for 'post_detail' with keyword arguments '{'pk': ''}' not found

孤人 提交于 2021-01-20 13:12:09
问题 I've been trying to get this app to work, I run: python manage.py runserver - & everything was fine, I was able to see the website, login as a superuser, make a comment, however in trying to view the post I started to get an error. Now, if I just go to the site, i get an error. I get this error message: File "/Users/homepage/opt/anaconda3/envs/MyDjangoEnv/lib/python3.8/site-packages/django/urls/resolvers.py", line 685, in _reverse_with _prefix raise NoReverseMatch(msg) django.urls.exceptions

Django Queryset foreign keys

做~自己de王妃 提交于 2021-01-07 03:12:39
问题 I am trying to get a queryset but it is not displaying anything. Basically, I want to get the Asset objects that are assigned via foreign key to an employee, which is a foreign key of the signed in user. views.py def get_queryset(self): assetlist = Asset.objects.filter(organisation__employee__user=self.request.user) print(assetlist) return assetlist models.py class Employee(models.Model): name = models.CharField("Employee Name", max_length=50, blank=False) email = models.CharField("Employee

Retrieve the same values ​whose data is there or exists and not the rest.In django

人走茶凉 提交于 2021-01-07 02:56:27
问题 I want to have a data that does not have any empty value in database or in row. I wrote like this in my code. faq = FAQ.objects.values('question','answer','field_id') this the output in my terminal {'question': None, 'answer': None, 'field_id': None} {'question': 'Test question', 'answer': '<p>Testsaddsf description</p>\r\n', 'field_id': 'TestTest'} i don't want None value data. 回答1: You can filter with the __isnull lookup [Django-doc]: faq = FAQ.objects.filter( question __isnull=False ,

How best to capture variables from within a for-loop in Django template

江枫思渺然 提交于 2021-01-07 02:48:49
问题 I have two querysets: type and age_group . type queryset: <QuerySet [<Type: Cat>, <Type: Dog>, <Type: Other>]> age_group queryset: <QuerySet [<AgeGroup: Young>, <AgeGroup: Baby>, <AgeGroup: Adult>, <AgeGroup: Senior>]> I loop through these from within my template form so that I can grab the pk when one has been selected, but I cannot capture the variable from within the for loop. How do I capture a variable from within a for loop when using Django? I want to capture pk for type and pk for age