Yes, photo_id is PK. So I shouldn't even give its value to the form in 
initial values I guess? 

Contest_id isn't PK, I want to select a random contest id from contest 
objects. (I decided to do this just now, my former code does not include 
this :) )
By the way, contest_id is foreign key in Photo. It is a PK in Contest. What 
am I doing wrong now? It gives the exact same error.

items = Contest.objects.all()
random_item = random.choice(items)

if request.method =='POST':
    data = {'contest_id': random_item.contest_id,
            'ownername': 'SUMMERSON',
            }
    form=PhotoForm(request.POST, initial=data)
    if form.is_valid():
        Photo = form.save()
else:
    form = PhotoForm()




28 Kasım 2017 Salı 00:04:25 UTC+3 tarihinde Matemática A3K yazdı:
>
> You shouldn't give the PK of new objects, leave that to Django. If 
> photo_id is the PK of Photo, don't set it, Django will on save.
>
> If the same goes for Contest, then do a "contest = Contest()" - create a 
> new contest - and assign it to the photo "photo.contest = contest", then 
> "photo.save" 
>
> On Mon, Nov 27, 2017 at 5:56 PM, Kubilay Yazoğlu <kyaz...@gmail.com 
> <javascript:>> wrote:
>
>> Hi. I'm trying to create a form to construct an object for my Photo 
>> class. All of the objects fields except the photo itself will be invisible 
>> to the user. So, the only thing can be seen in the form is the button to 
>> select and upload the photo.
>>
>> There is no problem with creating the form page. The problem is, giving 
>> the initial values for the rest of the fields. 
>> This is the relevant code in my view file:
>>
>>   
>>
>>     if request.method =='POST':
>>         data = {'photo_id': get_latest_photo_id_plusone(),
>>                 'contest_id': get_latest_contest_id_plusone(),
>>                 'ownername': 'SUMMERSON',
>>                 }
>>         form=PhotoForm(request.POST, initial=data)
>>         if form.is_valid():
>>             Photo = form.save()
>>     else:
>>         form = PhotoForm()
>>
>>
>> And the relevant functions:
>>
>>           
>>
>>     def get_latest_photo_id_plusone():
>>         max_rating = 
>> Photo.objects.all().aggregate(Max('photo_id'))['photo_id__max']
>>         return max_rating+1
>>
>>
>>     def get_latest_contest_id_plusone():
>>         max_rating 
>> =Photo.objects.all().aggregate(Max('contest_id'))['contest_id__max']
>>         return max_rating+1
>>
>>
>>
>> The error arises when I click on the send button after I select the 
>> image. It says: 
>>
>> IntegrityError at /photo/create/
>> NOT NULL constraint failed: photo_photo.contest_id_id
>>
>> But I've given the contest_id value. Haven't I? Where is the problem and 
>> how can I fix it?
>>
>> -- 
>> 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...@googlegroups.com <javascript:>.
>> To post to this group, send email to django...@googlegroups.com 
>> <javascript:>.
>> 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/57db225c-41bf-43d2-b9d8-ffb3174cddc7%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/django-users/57db225c-41bf-43d2-b9d8-ffb3174cddc7%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
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+unsubscr...@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/abb4fb48-2c9f-4453-823a-c9def3053b59%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to