Maniacal Science
Maniacal Science

Reputation: 181

Error passing User to ForeignKey in Django

I'm trying to pass a User model as the parameter for a ForeignKey in my models.py file, but I am getting the error TypeError: int() argument must be a string or a number, not 'User'.

Here are my files, please tell me what I'm doing wrong:

models.py

from django.db import models

class Lesson(models.Model):
    name = models.CharField(max_length=30)
    author = models.ForeignKey('auth.User')
    description = models.CharField(max_length=100)
    yt_id = models.CharField(max_length=12)
    upvotes = models.IntegerField()
    downvotes = models.IntegerField()
    category = models.ForeignKey('categories.Category')
    views = models.IntegerField()
    favorited = models.IntegerField()

    def __unicode__(self):
        return self.name

populate_db.py

from django.contrib.auth.models import User
from edu.lessons.models import Lesson
from edu.categories.models import Category

users = User.objects.all()

cat1 = Category(1, 'Mathematics', None, 0)
cat1.save()
categories = Category.objects.all()

lesson1 = Lesson(1, 'Introduction to Limits', users[0], 'Introduction to Limits', 'riXcZT2ICjA', 0, 0, categories[0], 0, 0)
lesson1.save() # Error occurs here

Upvotes: 1

Views: 1008

Answers (3)

eusid
eusid

Reputation: 769

from django.contrib.auth import User (forgot exact import call) author = models.ForeignKey(User)

Edit (additions): I would import the User the way that I stated and use 'author.category' for the other relationships. This has been resolved though by people who know more about Django than I do.

Upvotes: 0

BenH
BenH

Reputation: 894

You should use keyword arguments as well as simply the initialization it by using default field values.

class Lesson(models.Model):
    name = models.CharField(max_length=30)
    author = models.ForeignKey('auth.User')
    description = models.CharField(max_length=100)
    yt_id = models.CharField(max_length=12)
    upvotes = models.IntegerField(default=0)
    downvotes = models.IntegerField(default=0)
    category = models.ForeignKey('categories.Category')
    views = models.IntegerField(default=0)
    favorited = models.IntegerField(default=0)

    def __unicode__(self):
        return self.name


lesson1 = Lesson(name='Introduction to Limits', author=users[0], description='Introduction to Limits', yt_id='riXcZT2ICjA', category=categories[0])
lesson1.save()

Upvotes: 0

Using positional arguments here is very confusing and appears to be the cause.

I can reproduce your error by using positional arguments on a ForeignKey on one of my own models. Using kwargs solves the problem.

I'm not even interested in looking into why - I have never used confusing positional arguments to populate a model (seems like they would break ALL the time too with confusing messages if you ever modified your model)

Edit: or much worse, a silent error with input fields going to the wrong model fields over time.

Upvotes: 1

Related Questions