T
T
trytrytry2014-09-22 11:53:21
WordPress
trytrytry, 2014-09-22 11:53:21

How to create an object that has a relationship with another object?

I have a table User and Note. each note object contains a reference to a user object. I get an error when trying to create a second note object, the first instance for a registered user is created without problems, but he does not want to create further.

from django.db import models
from django.contrib.auth.models import User
class Note(models.Model):
        title = models.CharField(max_length=200)
    content = models.CharField(max_length=200)
    user = models.ForeignKey(User, unique=False)

def new_note(request):
    if request.method == 'POST':
        f = New_Note(request.POST)
        if f.is_valid():
            title = f.cleaned_data['title']
            content = f.cleaned_data['content']
            new = Note.objects.create(title=title,content=content,user=request.user)
            new.save();
            return HttpResponseRedirect(reverse('note:index'))
    else:
        f = New_Note()

    return render(request, 'note/new_note.html', {'f': f})

and when trying to create a second object, the following error occurs
column user_id is not unique

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Mr Crabbz, 2016-09-22
@Punkie

Remove All in one seo pack, install Yoast Seo.

D
Denis Yanchevsky, 2016-09-22
@deniscopro

Hello.
For what request?
No way. These links are added automatically .

I
Ivan, 2016-09-22
@IvanDanko

Google currently indexes 78 pages. And the site is on request "Boow Magazine".

R
Rrooom, 2014-09-22
@Rrooom

user = models.ForeignKey(User, unique=False)
remove unique altogether. Check the database to see if the user field has a unique requirement.
And there either directly clean, or migration.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question