views:

72

answers:

2

How to make entry.category to be instance of CategoryProxy? See code for details:

class Category(models.Model): pass

class Entry(models.Model):
    category = models.ForeignKey(Category)

class EntryProxy(Entry):
    class Meta:
        proxy = True

class CategoryProxy(Category):
    class Meta:
        proxy = True

entry = EntryProxy.objects.get(pk=1)
entry.category # !!! I want CategoryProxy instance here

Cast from Category to CategoryProxy is ok too, but I am not very familiar with ORM internals to properly copy internal state...

EDIT. Reason: I added method to CategoryProxy and want to use him:

EntryProxy.objects.get(pk=1).category.method_at_category_proxy()

EDIT 2. Currently I implemented it like this:

EntryProxy._meta.get_field_by_name('category')[0].rel.to = CategoryProxy

but it looks terrible...

A: 

Define a property category on EntryProxy that looks up the CategoryProxy by its id:

class EntryProxy(Entry):
    @property
    def category(self):
        cid = super(EntryProxy, self).category.id
        return CategoryProxy.objects.get(id=cid)

    class Meta:
        proxy = True
Bernd Petersohn
A: 

Joseph Spiros's answer to one of my questions might help you:

http://stackoverflow.com/questions/3176731/django-inheritance-and-permalinks/3183803#3183803

I'm not sure how it'll work with proxy models.

Lexo
One could probably achieve something comparable by adding a discriminator or content type field to the base model and hacking a customized model manager. However, I'm not sure this reflects what Vladimir intended. He may just need different views on his data, therefore he chose proxy models. I can imagine that he even wants to be able to define different categories of proxy models and expects that references between these models are resolved to proxies of the same category. This would not be possible if the type of the proxy is stored in the base model.
Bernd Petersohn