django-models

Compare date and datetime in Django

社会主义新天地 提交于 2020-06-24 22:23:46
问题 I have a model with a datetime field: class MyModel(models.Model): created = models.DateTimeField(auto_now = True) I want to get all the records created today. I tried: MyModel.objects.all().filter(created = timezone.now()) and MyModel.objects.all().filter(created = timezone.now().date()) But always got an empty set. What is the correct way in Django to do this? EDIT: It looks strange, but a record, created today (06.04.2012 23:09:44) has date (2012-04-07 04:09:44) in the database. When I'm

update for manytomany field

百般思念 提交于 2020-06-23 12:51:08
问题 i want to to update field whenever an instance been created i tried signals but it seems complicate to ManyToManyField class MobileCustomer(models.Model): customer = models.CharField(max_length=30) phone = models.CharField(max_length=13) mobile = models.ManyToManyField(MobileStorage,through='SelectMobile') class SelectMobile(models.Model): mobile = models.ForeignKey(MobileStorage,on_delete=models.CASCADE) item = models.ForeignKey(MobileCustomer,on_delete=models.CASCADE) quantity = models

update for manytomany field

若如初见. 提交于 2020-06-23 12:50:25
问题 i want to to update field whenever an instance been created i tried signals but it seems complicate to ManyToManyField class MobileCustomer(models.Model): customer = models.CharField(max_length=30) phone = models.CharField(max_length=13) mobile = models.ManyToManyField(MobileStorage,through='SelectMobile') class SelectMobile(models.Model): mobile = models.ForeignKey(MobileStorage,on_delete=models.CASCADE) item = models.ForeignKey(MobileCustomer,on_delete=models.CASCADE) quantity = models

Django - CheckboxSelectMultiple shows object representation instead of object's name

放肆的年华 提交于 2020-06-18 11:25:55
问题 So I am trying to have a list of checkboxes of cities, but instead of showing the cities' name, it shows this: How to make it shows the name instead of City object ? 回答1: In your model you have to include __str__ for python3 and unicode for python 2 For example python 3: class City(models.Model): name = forms.CharField(max_length=200, default="") def __str__(self): return self.name Python 2 class City(models.Model): name = forms.CharField(max_length=200, default="") def __unicode__(self):

Django - CheckboxSelectMultiple shows object representation instead of object's name

末鹿安然 提交于 2020-06-18 11:24:31
问题 So I am trying to have a list of checkboxes of cities, but instead of showing the cities' name, it shows this: How to make it shows the name instead of City object ? 回答1: In your model you have to include __str__ for python3 and unicode for python 2 For example python 3: class City(models.Model): name = forms.CharField(max_length=200, default="") def __str__(self): return self.name Python 2 class City(models.Model): name = forms.CharField(max_length=200, default="") def __unicode__(self):

Django - CheckboxSelectMultiple shows object representation instead of object's name

情到浓时终转凉″ 提交于 2020-06-18 11:23:45
问题 So I am trying to have a list of checkboxes of cities, but instead of showing the cities' name, it shows this: How to make it shows the name instead of City object ? 回答1: In your model you have to include __str__ for python3 and unicode for python 2 For example python 3: class City(models.Model): name = forms.CharField(max_length=200, default="") def __str__(self): return self.name Python 2 class City(models.Model): name = forms.CharField(max_length=200, default="") def __unicode__(self):

Django - CheckboxSelectMultiple shows object representation instead of object's name

狂风中的少年 提交于 2020-06-18 11:22:06
问题 So I am trying to have a list of checkboxes of cities, but instead of showing the cities' name, it shows this: How to make it shows the name instead of City object ? 回答1: In your model you have to include __str__ for python3 and unicode for python 2 For example python 3: class City(models.Model): name = forms.CharField(max_length=200, default="") def __str__(self): return self.name Python 2 class City(models.Model): name = forms.CharField(max_length=200, default="") def __unicode__(self):

Changing image in django user change form

心不动则不痛 提交于 2020-06-17 13:05:10
问题 I am making a django webapp where users can upload profile image. I have also created an EDIT PROFILE page by using the default USERCHANGEFORM . But the problem is, that I cannot update the profile picture from that form. I can delete it but not upload new one? Help needed. This is my USER CREATION FORM : class SignUpForm(UserCreationForm): photo = forms.ImageField(required=False) bio = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Enter your bio'})) designation = forms

How to edit user's profile page on django?

℡╲_俬逩灬. 提交于 2020-06-17 08:05:31
问题 My django project has multiple functions, one of them lets the user update its profile(User model"first_name, username and email" Profile model" bio and profile picture") this used to perfectly work until I added a follow sistem, it is like the whole Profile and User model doesnt exist anymore so when trying to edit those fields, the code returns a AttributeError: 'User' object has no attribute 'profile' error, saying this line of code on the views.py file is wrong form1 = UpdateProfileForm

what is related_name and related_query_name in django?

青春壹個敷衍的年華 提交于 2020-06-17 00:13:11
问题 i have an issue with the code in django framework regarding to related_name and related_query_name in django. please django expert explain the related_name in django, the code is below: related_name='+' 回答1: Related Name Django maintains backward relation on each object for easy access to related objects. Suppose you have two models named "School" and "Student" and one school can have multiple students. So you will have model definition something like this class School(models.Model): name =