On Mon, Nov 27, 2017 at 7:18 PM, Kubilay Yazoğlu <kyazogluu@gmail.com> wrote:
Nice try but I'm still getting this error:IntegrityError at /photo/create/
NOT NULL constraint failed: photo_photo.contest_id_idI tried with both 'contest': random_item and 'contest_id': random_item.contest_id. (I don't know why you wrote contest instead of contest_id. Field name is contest_id)
That's because what you are passing is empty and the field is required. Are you sure that you have Contest objects?
There is a convention in Django (models and ORM), "object.foreign_key" gives you the object, while "object.foreign_key_id" gives you the id of that object (and is what it is stored in the database). You should name your field only "contest" for avoiding confusions.
--
28 Kasım 2017 Salı 01:03:50 UTC+3 tarihinde Matemática A3K yazdı:items = Contest.objects.all()
random_item = random.choice(items)
if request.method =='POST':
data = {'contest': random_item,
'ownername': 'SUMMERSON',
}
form=PhotoForm(request.POST, initial=data)
if form.is_valid():
photo = form.save() # Instances go in lowercase, otherwise you are doing it to the class
else:
form = PhotoForm()
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com .
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users .
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/87731060- .75a6-44c6-8d1f-56cba6d22278% 40googlegroups.com
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2BFDnhK%3Dkkjra0nXZ380%3D667xT0JwD8a2rmaPy0g%3DXEPU9Rx9Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment