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 8299367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:07:32+00:00 2026-06-08T16:07:32+00:00

I have a model #models.py class BaseModel(model.Models) some_field = … class Proxy_1(BaseModel) class Meta:

  • 0

I have a model

#models.py
class BaseModel(model.Models)
    some_field = ...

class Proxy_1(BaseModel)
    class Meta:
        proxy=True

class Proxy_2(BaseModel)
    class Meta:
        proxy=True

class Proxy_3(BaseModel)
    class Meta:
        proxy=True

in my view

#views.py
    #check the field and based on the value
    #choose appropriate Proxy model
    if request.POST['some_field'] == '1'
        # Table_Name = Proxy_1   ??????? ---HERE ---
    if request.POST['some_field'] == '2'
        # Table_Name = Proxy_2   ??????? ---HERE ---
    if request.POST['some_field'] == '3'
        # Table_Name = Proxy_3   ??????? ---HERE ---

    foo = Table_Name.objects.get_or_create(...)

I don’t really know how to do it…
Obviously I can write foo = Table_Name.objects.get_or_create(…) in between request.POST calls, but it would bee too long and hard to debug in the future.

My ideas was also about creating a function in models.py to check that, but again don’t know how to do that, or may be somehow check it in my html template.

Thank you

  • 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-06-08T16:07:34+00:00Added an answer on June 8, 2026 at 4:07 pm

    A simple solution is creating a form for your model and override the save method to return the proper proxy instance:

    # forms.py
    PROXIES = {'1': Proxy_1, '2': Proxy_2, '3': Proxy_3}
    class BaseModelForm(forms.ModelForm):
        class Meta:
            model = BaseModel
    
        def save(self, commit=True):
            instance = super(BaseModelForm, self).save(commit=True)
            if instance.some_field in PROXIES.keys():
                if commit:
                    return PROXIES[instance.some_field].objects.get(pk=instance.pk)
                return PROXIES[instance.some_field](**self.cleaned_data)
            return instance
    
    # views.py
    def myview(request):
        form = BaseModelForm(request.POST or None)
        if form.is_valid():
            instance = form.save()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model: class Example(models.Model): unique_hash = models.CharField(max_length=32,unique=True) content = models.FileField(upload_to='source',blank=True,verbose_name=HTML Content File)
I have two models: class Actor(models.Model): name = models.CharField(max_length=30, unique = True) event =
I have a model: class MyModel(models.Model): id = models.IntegerField(primary_key=True) recorded_on = models.DateField() precipitation =
I have these models: class Gallery(models.Model): HeadImage = models.ImageField(upload_to=gallery,blank=True,null=True) class News(Gallery): Name=models.CharField(max_length=100) #some other
I have 3 model classes: class Team(models.Model): name = models.CharField(max_length=100, default=, blank=True, null=True) number
I have three model classes that look as below: class Model(models.Model): model = models.CharField(max_length=20,
i've a problem with my tabularinline field. I have model like this class Product(models.Model):
I have two models: class Contact(models.Model): name = models.CharField(max_length=255) class Campaign(models.Model): contact = models.ForeignKey(Contact,
I have these models: class Model1(models.Model): ... class Model2(models.Model): m1 = models.OneToOneField(Model1, related_name='m2') ...
I have these models: class UserProfile(models.Model): user = models.OneToOneField(User) #etc class Organization(models.Model): users =

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.