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

  • Home
  • SEARCH
  • 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 7553097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:57:21+00:00 2026-05-30T10:57:21+00:00

There are three models: House_Type, House_Option, and Order The House_Type model has 2 fields:

  • 0

There are three models: House_Type, House_Option, and Order

The House_Type model has 2 fields: id and name

House_Option has 3 fields: id, name and type where type is a foreign key linked to House_Type.

and finally, Order consists of many fields, one of which is a ManytoMany field called “choice” that links to House_Option

The way this works is that House_Type has different “types” of houses: for example, apartment, condo, detached house, semi detached house etc..

House_Option has all the possible options for each type: so for example, for the “apartment” type, you have option 1 located on street X, option 2 located on street Y etc..

In the Order model, the user has to choose one “option” of each house “type”. So they must pick one apartment option, one house option etc.. Because this is a ManytoMany field, this is possible. However my question is: How do I prevent the user from choosing TWO “apartment” options for example. How do I restrict them to only choosing one (or none) of each?

I was trying to create a def(clean) in the Order model:

        def clean(self):
            if self.choice.house_option_type.count() > 1:
                    raise ValidationError('Custom Error Message')

This however returns an attribute error: ‘ManyRelatedManager’ object has no attribute ‘house_option_type’

Any ideas?

  • 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-30T10:57:22+00:00Added an answer on May 30, 2026 at 10:57 am

    Manage ManyToMany relationship through an explicitly defined model, where both foreign keys are unique for each order. You can do this using unique_together to impose uniqueness constraint on the many to many relationship across same types.

     class House_Type(models.Model):
          name = models.CharField(...)
    
    
     class House_Option(models.Model):
          name = models.CharField(...)
          type = models.ForeignKey(House_Type)
    
     class Order(models.Model):
          ...
          choices = models.ManyToManyField(House_Option, through='Order_options')
          ...
    
     class Order_options(models.Model):
          class Meta:
              unique_together = ('order', 'option__type')
    
          ...
          order = models.ForeignKey(Order)
          option = models.ForeignKey(House_Option)
          ...
    

    Edit, updated syntax and correction.

    Yeah it looks like unique_together is applied as DB constraint on the table and won’t work across tables. So forget about the above approach.

    Still I think the following should work:

    If you simply override validate_unique on Order_options and implement uniqueness logic yourself, while being careful how to handle existing and non existent case it should work.

    from django.core.exceptions import ValidationError, NON_FIELD_ERRORS
    
    class Order_options(models.Model):
        ...
        def validate_unique(self, exclude = None):
            super(Order_options, self).validate_unique(exclude)
    
            options = { 'order__id' : self.order.id, 'option__type' : 'self.option.type' }
            objs = Order_options.objects.exclude(id=self.id) if self.id else Order_options.objects
            if objs.filter(**options).exists():
                raise ValidationError({NON_FIELD_ERRORS: ['Error: {0} option type already exists'.format(self.option.type)]})
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a CAkePHP 1.2 site. I've got three related Models/tables: A Comment has
There are three thread models that are used in thread scheduling implementations usually done
I have three models: class ReleaseItem < ActiveRecord::Base has_many :pack_release_items has_one :pack, :through =>
I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through =>
I have three models: class Address < ActiveRecord::Base has_many :jobs end class Category <
I have three models that look something like this: class Bucket < ActiveRecord::Base has_many
Let's assume three models, standard joins: class Mailbox < ActiveRecord::Base has_many :addresses has_many :domains,
Using Django's built in models, how would one create a triple-join between three models.
I've created a class library that contains three entity models for three databases that
Is there an easy way to convert between color models in Java (RGB, HSV

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.