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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:10:38+00:00 2026-06-14T10:10:38+00:00

So basically my app has users of course and each user can create 5

  • 0

So basically my app has users of course and each user can create 5 instances of “ModelA”. Simple enough, but there is also “ModelB” with a relation to “ModelA” and the user model. I want the user to be able to create a total of 15 “ModelB” instances, but each “ModelA” instance can only have 5 “ModelB” instances tied to it?

Any tips?

The way I handle the first part for 5 “ModelA” instances per user is like this:

def clean(self):
        new_instance = self.__class__
        if (new_instance.objects.count() > 4):
            raise ValidationError(
                "Users may only create 5 %s." % new_instance.verbose_name_plural
            )
        super(ModelA, self).clean()

Thanks

EDIT:(Built in Django Users functionality assumed)

class ModelB(models.Model):
    user = models.ForeignKey(User)
    modelA = models.ForeignKey('ModelA')
    other_field = models.CharField(max_length=50)

class ModelA(models.Model):
    user = models.ForeignKey(User)
    other_field = models.CharField(max_length=50)

Basically The user can create 5 ‘ModelA’ instances and for each of those instances they can create 3 ‘ModelB’ instances.

How can I do this within the model logic?

thanks

  • 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-14T10:10:40+00:00Added an answer on June 14, 2026 at 10:10 am

    Does this work?

    class ModelB(models.Model):
      user = models.ForeignKey(User)
      modelA = models.ForeignKey('ModelA', related_name = 'modelbs')
      other_field = models.CharField(max_length=50)
    
      def clean(self):
        if (self.modelA.modelbs.all().count() > 2):
            raise ValidationError(
                "ModelA may create may only create 3 modelBs "
            )
        super(ModelB, self).clean()
    
    
    class ModelA(models.Model):
      user = models.ForeignKey(User, related_name = 'modelas')
      other_field = models.CharField(max_length=50)
    
      def clean(self):
        if (self.user.modelas.all().count() > 2):
            raise ValidationError(
                "User may create may only create 3 modelAs "
            )
        super(ModelA, self).clean()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I want to check the extended permissions the user has granted my app.
How can I build an android app which basically get the inputs from user
I have a simple db.Model , that has one of the fields db.ListProperty(users.User) For
basically my app has a loginScreen and once logged u can go through many
Basically, I have a users model in my rails app, and a fanship model,
I have to create an educational app where it is basically like a powerpoint
I am building a video streaming app that allows users to create playlists and
heres the quick background of the app Basically.. Multiple users(usually only one) per machine
Basically, when users navigate through my web app using /#/blah/blah , I want an
I'm developing an app (basically an intranet) which has a few small sets of

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.