Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6322265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:17:57+00:00 2026-05-24T16:17:57+00:00

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

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T16:17:58+00:00Added an answer on May 24, 2026 at 4:17 pm

    To switch from a model class to a proxy class without hitting the database:

    class EntryProxy(Entry):
        @property
        def category(self):
            new_inst = EntryProxy()
            new_inst.__dict__ = super(EntryProxy, self).category.__dict__
            return new_inst
    

    edit: the snippet above seems not working on django 1.4.

    Since django 1.4, I take all value fields manually like this:

    class EntryProxy(Entry):
        @property
        def category(self):
            category = super(EntryProxy, self).category
            new_inst = EntryProxy()
            for attr in [f.attname for f in category.__class__._meta.fields] + ['_state']:
                setattr(new_inst, attr, getattr(category, attr))
            return new_inst
    

    To switch from a queryset to a child proxy class without hitting database:

    class CategoryProxy(Category):
        @property
        def entry_set(self):
            qs = super(CategoryProxy, self).entry_set
            qs.model = EntryProxy
            return qs
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an entry.rb model and I'm trying to make a semi-complicated validation. I
Models: User has_many Categories, has_many Entries Category has_many Entries Entry belongs_to both Category and
Is it possible to make a Telerik RadEditor single-line entry only? For example, in
I am currently trying to make a sectioned table like this: Section 1: Entry
Make a new AS3 Document in Flash, paste in the following code and run
To make a JavaScript class with a public method I'd do something like: function
I want to make an entry in the log if my servlet throws ServletException
Consider an entity, Entry, that contains a collection of another entity (many-to-many), Category, such
I would like to make a django custom tag to display 10 entry titles
Google has suggestion come up when you make a typo entry,how do they do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.