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!
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).
Either just give the field a default=...
value, or set it to null=True
.
null=True
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With