bdeniker
bdeniker

Reputation: 1035

Accessing Django custom models through Manager returns empty set

From all I've read, it appears this should Just Work, but it doesn't.

I have a custom model:

from django.db import models
from django.contrib.auth.models import *

class Feed(models.Model):
    user = models.ForeignKey(User, blank=True, null=True)
    link = models.CharField(max_length=200)
    startDate = models.CharField(max_length=8)
    endDate = models.CharField(max_length=8)

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

    def __init__(self, link, sDate, eDate, user=None):
        super(Feed, self).__init__()
        self.link = link
        self.startDate = sDate
        self.endDate = eDate
        self.user = user

And I'm also using the User model included in 'django.contrib.auth.models'.

When I create a feed, e.g.

feed = Feed(link, sDate, eDate)
feed.save()

(or a similar one with a user specified) it appears to store it in the database (I get its PK which keeps incrementing), but 'Feed.objects.all()' returns an empty list. Trying to filter by an existing pk also returns an empty list and trying to get() an existing pk gives me the following error

TypeError: __init__() takes at most 5 arguments (6 given)

Looking at how I should be retrieving objects from custom models, it appears that I've done everything I should, but that is clearly not the case...

Upvotes: 0

Views: 468

Answers (2)

dgel
dgel

Reputation: 16796

Whoa.

Why are you overriding your model's __init__? There are very few good reasons to do this, and if you do, you must absolutely keep the interface the same- because that __init__ is called every time django pulls one of your models from the db (which is why you get the error when you call .get())

What are you hoping to accomplish with your __init__?

You should probably just delete your __init__ and then you can create Feed objects the normal, django way:

feed = Feed(link=link, startDate=sDate, endDate=eDate)

That line will create the correct feed object you want.

Upvotes: 2

jm_toball
jm_toball

Reputation: 1217

Did you try named arguments, e.g.

feed = Feed(link=link, startDate=sDate, endDate=eDate)

How did you use get()? It should also be used with named arguments, e.g.:

Feed.objects.get(pk=6)

Upvotes: 0

Related Questions