views:

21

answers:

1

In my application, i have a model definition like:

class SomeModel(models.Model):
    someFK = Models.ForeignKey(SomeModel, null=True, blank=True)
    otherFK = Models.ForeignKey(OtherModel, null=True, blank=True)
    ...

This model keep data about Logs, and there are more than 1 kind of model to be logged, so i place a FK for each related Model. For each record, only one of the FK's is used, other FK's set NULL = True.

But i do not wish to alter my model when i need to log some other thing. So i alter my model so:

class SomeModel(models.Model):
    content_type = models.ForeignKey(ContentType)
    content_id = models.IntegerField()

So i used following to get what i want:

content_type.get_object_for_this_type(id=content_id)

Its ok. But when admin.py comes into play, it causes problem, because i have 1.000.000+ data for some related models to be logged, so i need to use raw_id_fields. Since i do not have FK for each related model, i must use ContentType model and content_id, but i do not know how to do that using ContentType?

A: 

I know this doesn't answer your question exactly, but you should be using generic relations instead of multiple foreignkeys. Then you can achieve the end-goal of what you need.

pziddy
As i said, my models do not contain FK to each model, but a Fk to xontent type model, a field for id of related object, and a genetricFK. But, since genericFk has no field representation in db, it have a lot of limitations, and i can not use GenericFK in admin.py
FallenAngel