james_dean
james_dean

Reputation: 1517

Django Foreign Key Assignment

I am pretty new to Django and this has me stumped.

I am working on a scheduling tool and have two models so far:

#----------------------------------------------------------------------------------

class ReccurenceEvent(models.Model):
    """
    A recurrence event defined an event which recurses over a period of time. The pattern of the recursion
    is defined within the rec_type attribute.
    """
    event_id = models.IntegerField(primary_key=True)
    start_date = models.DateTimeField()
    end_date = models.DateTimeField()
    text = models.CharField(max_length=30)
    rec_type = models.CharField(max_length=32)
    event_length = models.BigIntegerField(null=True)

#----------------------------------------------------------------------------------

class Event(models.Model):
    """
    A an event represents an independent calender event. 
    If the event relates to a series, the p_id points to a particular ReccurencePattern
    """
    event_id = models.IntegerField(primary_key=True)
    start_date = models.DateTimeField()
    end_date = models.DateTimeField()
    text = models.CharField(max_length=30)
    rec_type = models.CharField(max_length=32)
    event_length = models.BigIntegerField(null=True)

    event_pid = models.ForeignKey(ReccurenceEvent, null=True)

In certain circumstances, I want to assign a parent event id (event_pid) to an event but this should be an integer value and not a reference to the object.

When I do the following:

e.event_pid = event_pid

I get the following message:

Cannot assign "u'1359741862566'": "Event.event_pid" must be a "ReccurenceEvent" instance.

Ok, but if a provide a recurrence instance with:

e.event_pid = ReccurenceEvent.objects.get(event_id = event_pid)

I am not storing the integer value that I need in the response.

Could someone point out where I am going wrong?

Upvotes: 4

Views: 5954

Answers (2)

aychedee
aychedee

Reputation: 25619

You can access the id of the RecurrenceEvent in your view by looking at e.event_pid.event_id.

But I think you are misnaming the fields of your model.

class ReccurenceEvent(models.Model):

    event_id = models.IntegerField(primary_key=True)
    start_date = models.DateTimeField()
    end_date = models.DateTimeField()
    text = models.CharField(max_length=30)
    rec_type = models.CharField(max_length=32)
    event_length = models.BigIntegerField(null=True)

class Event(models.Model):

    start_date = models.DateTimeField()
    end_date = models.DateTimeField()
    text = models.CharField(max_length=30)
    rec_type = models.CharField(max_length=32)
    event_length = models.BigIntegerField(null=True)

    reccurance = models.ForeignKey(ReccurenceEvent, null=True)

Then you just access the attributes of RecurranceEvent via the event. Also, you don't need to explicitly add a primary key. Django does it for you. It's available at .id and .pk.

so e.reccurance.id would be the integer you need in your view/template.

Upvotes: 2

dm03514
dm03514

Reputation: 55972

If you look in your database the foreign key IS an integer value! but becuase of the django orm you can interact in terms of objects and not numbers

to get the integer of the ForeignKey you should be able to access it through event_id

e.event_pid.event_id

Upvotes: 3

Related Questions