django-models

Django cleaned_data.get('obj') method returns none

久未见 提交于 2020-05-27 07:24:46
问题 I'm doing a little password confirmation form in django. But Im confused as the self.cleaned_data.get('confirm_password') always returns none and hence my passwords never match to one another. Heres the clean method in the forms.py def clean_password(self): password = self.cleaned_data.get("password") confirm_password = self.cleaned_data.get("confirm_password") if password != confirm_password: print(password) print(confirm_password) raise forms.ValidationError( "Password and password

Django cleaned_data.get('obj') method returns none

佐手、 提交于 2020-05-27 07:23:49
问题 I'm doing a little password confirmation form in django. But Im confused as the self.cleaned_data.get('confirm_password') always returns none and hence my passwords never match to one another. Heres the clean method in the forms.py def clean_password(self): password = self.cleaned_data.get("password") confirm_password = self.cleaned_data.get("confirm_password") if password != confirm_password: print(password) print(confirm_password) raise forms.ValidationError( "Password and password

Django cleaned_data.get('obj') method returns none

微笑、不失礼 提交于 2020-05-27 07:23:04
问题 I'm doing a little password confirmation form in django. But Im confused as the self.cleaned_data.get('confirm_password') always returns none and hence my passwords never match to one another. Heres the clean method in the forms.py def clean_password(self): password = self.cleaned_data.get("password") confirm_password = self.cleaned_data.get("confirm_password") if password != confirm_password: print(password) print(confirm_password) raise forms.ValidationError( "Password and password

Using `issubclass()` with Django models

时光怂恿深爱的人放手 提交于 2020-05-26 14:29:28
问题 I have some Django models, say class Foo(models.Model): class Meta: abstract = True class Bar(Foo) pass I would like to be able to find all models inheriting from Foo, in order to perform a task with them. It should be easy, like from django.db import models from myapp.models import Foo for model in models.get_models(): if issubclass(model, Foo): do_something() Alas, this does not work, since issubclass(Bar, Foo) reports False , probably as a result of the inner working of the Django

Django: define a name for reverse ForeignKey

随声附和 提交于 2020-05-26 10:36:28
问题 I have two models: class Foo(models.Model): foo_field = ... class Bar(models.Model): foo = models.ForeignKey(Foo, on_delete=models.CASCADE) bar_field = ... and I can access all Bar instances related to a Foo with: Foo.bar_set.all() Is there a way to change the 'reverse name', like in ManyToManyField, so that I can write: Foo.bars.all() ? 回答1: yes, using related_name class Foo(models.Model): foo_field = ... class Bar(models.Model): foo = models.ForeignKey(Foo, related_name="bars", on_delete

Django: define a name for reverse ForeignKey

旧城冷巷雨未停 提交于 2020-05-26 10:36:23
问题 I have two models: class Foo(models.Model): foo_field = ... class Bar(models.Model): foo = models.ForeignKey(Foo, on_delete=models.CASCADE) bar_field = ... and I can access all Bar instances related to a Foo with: Foo.bar_set.all() Is there a way to change the 'reverse name', like in ManyToManyField, so that I can write: Foo.bars.all() ? 回答1: yes, using related_name class Foo(models.Model): foo_field = ... class Bar(models.Model): foo = models.ForeignKey(Foo, related_name="bars", on_delete

Order Django Query Results by Foreign Key

送分小仙女□ 提交于 2020-05-26 10:17:06
问题 I have a model that is set up as follows: class Log(models.Model): name = models.ForeignKey(User) date = models.DateField() time = models.TimeField() I know this does not work, but is there any other way I can run a query something like this: Logs.objects.filter(date=someDate).order_by('name__last_name') I just need the final result to be a QuerySet ordered by the last name of the User that is related by the ForeignKey . I'm really at my wits end about this one. Anything would help: some

Add a friend system on django

岁酱吖の 提交于 2020-05-26 09:09:25
问题 I have been trying to add friend system in which the user can add and remove friends (other users), after finishing with the code, I found an error that when the logedin user tries to add a friend from other user's profile, the add friend button redirects to the logedin user profile making it imposible to add a new friend, it can just add himself as a friend. I personally think the error is on the views.py profile view. views.py (profile shows user's profile and change_friend is the one that

Django model method - create_or_update

久未见 提交于 2020-05-25 06:09:20
问题 Similar to get_or_create, I would like to be able to update_or_create in Django. Until now, I have using an approaching similar to how @Daniel Roseman does it here. However, I'd like to do this more succinctly as a model method. This snippet is quite old and I was wondering if there is a better way to do this in more recent version of Django. 回答1: See QuerySet.update_or_create (new in Django 1.7dev) 回答2: There is update_or_create , eg:: obj, created = Person.objects.update_or_create( first

Django - How to make a form for a model's foreign keys?

≯℡__Kan透↙ 提交于 2020-05-25 04:31:50
问题 Here's what I'm trying to do. I'm wondering if someone can suggest a good approach: models.py: class Color(models.Model): name = models.CharField(... class Speed(models.Model): name = models.CharField(... class Dog(models.Model): name = models.CharField(... color = models.ForeignKey(Color... speed = models.ForeignKey(Speed... class DogRequest(models.Model): dog = models.ForeignKey(Dog... request_time = models.DateTimeField() Now I want to have a page where a user can enter or edit a