Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Make ContentType Not Required

I have this model:

class Auth(models.Model):
    TYPES = (
         ('agent', 'Agent'),
         ('broker', 'Broker'),
     )
    user = models.ForeignKey(User, unique=True)
    type = models.CharField(max_length=20, choices=TYPES)
    applied = models.BooleanField()
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField(db_index=True)
    content_object=generic.GenericForeignKey('content_type', 'object_id')

and whenever I do something like this:

User.objects.create_user(username="myuser", password="myuser", email="myemail.com")
u = User.objects.get(username="myuser")
profile = Auth(user=u)
profile.save()

of course I will receive the error:

IntegrityError: (1048, "Column 'content_type_id' cannot be null")

For my purposes, I can't avoid contenttypes because Auth is a class where a Broker and Agent class inherits from, which allows me to do multiple custom profiles.

I was wondering if there was a way in which the content type would not be required.

Thanks in advance!

like image 482
yretuta Avatar asked Apr 08 '11 08:04

yretuta


4 Answers

content_type = models.ForeignKey(ContentType, null=True, blank=True)

null=True makes it optional in the data model, blank=True makes it optional when using admin form (otherwise you'll get validation error).

like image 157
vartec Avatar answered Sep 19 '22 12:09

vartec


Either just give the field a default=... value, or set it to null=True.

like image 30
bradley.ayers Avatar answered Sep 20 '22 12:09

bradley.ayers


null=True

like image 22
Daniel Roseman Avatar answered Sep 22 '22 12:09

Daniel Roseman


OK, I encontered the same problem today with django 1.8

models.py:

class Author(models.Model):
    user = models.OneToOneField(User)
    name = models.CharField(max_length=50)
    sig = models.CharField(max_length=150)
    up_num = models.IntegerField(default=0)

    def __unicode__(self):
        return self.user.username

views.py:

def register(request):
    if request.method == 'POST':
        form = registerFrom(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']
            name = form.cleaned_data['name']
            sig = form.cleaned_data['sig']
            u = User.objects.create_user(username, email, password)

            add_blog_permission = Permission.objects.create(codename='add_blog')
            change_blog_permission = Permission.objects.create(codename='change_blog')
            delete_blog_permission = Permission.objects.create(codename='delete_blog')
            add_comment_permission = Permission.objects.create(codename='add_comment')
            add_message_permission = Permission.objects.create(codename='add_message')
            delete_message_permission = Permission.objects.create(codename='delete_message')
            u.user_permissions.add(add_blog_permission, change_blog_permission, delete_blog_permission, add_comment_permission, add_message_permission, delete_message_permission)


            Author.objects.create(user=u, name=name, sig=sig)
            return HttpResponseRedirect(reverse('login', args=()))
    form = registerFrom()
    return render(request, 'register.html', {'form': form})

on register page after run server, when submit the data:

IntegrityError: (1048, "Column 'content_type_id' cannot be null")

finally, I found there are errors in my code:

add_blog_permission = Permission.objects.create(codename='add_blog')

should be:

add_blog_permission = Permission.objects.get(codename='add_blog')
like image 39
yuyan Avatar answered Sep 19 '22 12:09

yuyan