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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:16:26+00:00 2026-06-09T13:16:26+00:00

In Django, I have the following models.py class Product(RandomPrimaryIdModel): feature1 = models.CharField(max_length=20, blank=True, null=True)

  • 0

In Django, I have the following models.py

class Product(RandomPrimaryIdModel):
  feature1 = models.CharField(max_length=20, blank=True, null=True)
  feature2 = models.CharField(max_length=20, blank=True, null=True)
  feature3 = models.CharField(max_length=20, blank=True, null=True)

class Mattress(Product):
  category_type = models.CharField(max_length=50)
  size = models.CharField(max_length=5)

  def category(self):
    return "bedding"
  category = property(category)

I have the following views.py file

def update(request, id):
  product = Product.objects.get(id=id)
  ...

In this method, update, can I call a method defined in the “Mattress” model from the Product model. For example, I want to write: if product.type == “mattress” where type has been defined in the Mattress Model and Mattress is a sub-model of Product.

  • 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-09T13:16:28+00:00Added an answer on June 9, 2026 at 1:16 pm

    Your example seems to sit between two different ways you can go, but is currently not correct. What is happening is that you are creating two tables: Product, and Mattress, and they are completely unrelated. Regardless of the fact that Mattress subclasses Product, it is just inheriting its structure. You cannot query anything in the Product table about a mattress because a mattress is in the Mattress table.

    One way to go is to consider a Product just abstract, to be subclassed by actual products:

    class Product(RandomPrimaryIdModel):
        class Meta:
            abstract=True
    

    This will prevent a Product table from being created. Then you would directly query a mattress via: Mattress.objects.filter()

    But this seems a bit limiting in terms of introducing many types of products, and having to manage different tables for them. The other way to go is to use a Product table, but use generic relations to support attaching any type of other table as a content object:

    from django.db import models
    from django.contrib.contenttypes.models import ContentType
    from django.contrib.contenttypes import generic
    
    class Product(RandomPrimaryIdModel):
    
        feature1 = models.CharField(max_length=20, blank=True, null=True)
        feature2 = models.CharField(max_length=20, blank=True, null=True)
        feature3 = models.CharField(max_length=20, blank=True, null=True)
    
        content_type = models.ForeignKey(ContentType)
        object_id = models.PositiveIntegerField()
        content_object = generic.GenericForeignKey('content_type', 'object_id')
    

    With this, you would be able to set the content_object to be a Mattress instance. You can then use the ContentType to query:

    p_type = ContentType.objects.get(name="mattress")
    Product.objects.filter(content_type=p_type)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Django, I have the following models.py class Product(RandomPrimaryIdModel): title = models.CharField(max_length=20, blank=True, null=True)
I have the following abstract Django models: class Food(models.Model): name = models.CharField(max_length=100) class Meta:
I have the following Django models: - class Company(models.Model): name = models.CharField(max_length=50) is_active =
I have following models class Artist(models.Model): name = models.CharField(max_length=200, unique=True) photo = models.CharField(max_length=250) views
I have the following model: from django.db import models class State(models.Model): name = models.CharField(max_length=30)
I have the following models: class Bill(models.Model): date = models.DateTimeField(_(Date of bill),null=True,blank=True) class Item(models.Model):
In my Django App I have the following model: class SuperCategory(models.Model): name = models.CharField(max_length=100,)
I have following models relation: class Section(models.Model): section = models.CharField(max_length=200, unique=True) name = models.CharField(max_length=200,
in a django-tastypie app I have the following Django-models: class Car(models.Model): name=models.CharField('name',max_length=64) class CarTrack(models.Model):
I have the following model. from django.db import models class Client(models.Model): postcode = models.CharField(max_length=10)

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.