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 <[email protected]>
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 [email protected].
> To post to this group, send email to [email protected].
> 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 [email protected].
To post to this group, send email to [email protected].
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%2BFDnhJM1k8kk4ZudQsaH9QpsnAAHHHDbMYwLB09oTT0L2bfNg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to