0

That's my models.py:

class User(models.Model):
    email = models.EmailField()

    def __unicode__(self):
        return str(self.email)


class Link(models.Model):
    link = models.URLField()
    users = models.ManyToManyField(User, through='ShortURL')


class ShortURL(models.Model):
    link = models.ForeignKey(Link)
    user = models.ForeignKey(User)
    short_end = models.CharField(max_length=20)
    counter = models.IntegerField() 

adding users works just fine:

>>> user = User.objects.create("john_doe@planetearth.com")

I get integrity error when I try to add link:

>>> link = Link.objects.create("stackoverflow.com")
IntegrityError: shortener_link.short_end may not be NULL

What am I missing?

andrew_vvv
  • 75
  • 1
  • 1
  • 5

1 Answers1

0

You need to create a link like this:

Link.objects.create(link="stackoverflow.com", user = request.user, short_end = 'stack', counter = 0)

since all of those fields are required.

JeffS
  • 2,647
  • 2
  • 19
  • 24
  • now I get another error: Traceback (most recent call last): File "", line 1, in File ".../django/db/models/manager.py", line 137, in create return self.get_query_set().create(**kwargs) File ".../django/db/models/query.py", line 375, in create obj = self.model(**kwargs) File ".../django/db/models/base.py", line 367, in __init__ raise TypeError("'%s' is an invalid keyword argument for this function" % kwargs.keys()[0]) TypeError: 'counter' is an invalid keyword argument for this function – andrew_vvv Jul 07 '12 at 01:33
  • That error would imply that you don't have a counter field defined in models – JeffS Jul 07 '12 at 01:40
  • Yes, but it is defined. In ShortURL class. I'm confused. – andrew_vvv Jul 07 '12 at 01:54
  • [link]https://docs.djangoproject.com/en/dev/topics/db/models/ the beatles example works, I'm a moron for not looking up documentation first. – andrew_vvv Jul 07 '12 at 02:25