views:

10

answers:

2

This is are my models i want to relate. i want for collection to appear in the form of occurrence.

class Collection(models.Model):
    id = models.AutoField(primary_key=True, null=True)
    code = models.CharField(max_length=100, null=True, blank=True)
    address = models.CharField(max_length=100, null=True, blank=True)
    collection_name = models.CharField(max_length=100)

    def __unicode__(self):
        return self.collection_name

    class Meta:
        db_table = u'collection'
        ordering = ('collection_name',)

class Occurrence(models.Model):
    id = models.AutoField(primary_key=True, null=True)
    reference = models.IntegerField(null=True, blank=True, editable=False)
    collection = models.ForeignKey(Collection, null=True, blank=True, unique=True),
    modified = models.DateTimeField(null=True, blank=True, auto_now=True)
    class Meta:
         db_table = u'occurrence'

Every time i go to check the Occurrence object i get this error

 TemplateSyntaxError at /admin/hotiapp/occurrence/
 Caught an exception while rendering: column occurrence.collection_id does not exist
 LINE 1: ...LECT "occurrence"."id", "occurrence"."reference", "occurrenc..

And every time i try to add a new occurrence object i get this error

 ProgrammingError at /admin/hotiapp/occurrence/add/
 column occurrence.collection_id does not exist
 LINE 1: SELECT (1) AS "a" FROM "occurrence" WHERE "occurrence"."coll...

What am i doing wrong? or how does ForeignKey works?

A: 

The problem is that you have not updated your database table definition since adding the ForeignKey. syncdb doesn't do this for you, as the documentation clearly states. You need to update the SQL manually, or use a tool like South.

Daniel Roseman
yep that was the problem. thanks
Daniel Garcia
A: 

Are you sure you mean

collection = models.ForeignKey(Collection, null=True, blank=True, unique=True),

Nullable and Unique? This may not be possible in some databases.

Generally, the unique constraint doesn't seem to make much sense here.

Are you trying to force a 1-to-1 relationship? Use the OneToOneField. http://docs.djangoproject.com/en/1.1/ref/models/fields/#django.db.models.OneToOneField

S.Lott