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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:24:39+00:00 2026-05-11T21:24:39+00:00

I realize this has been asked before, but I wasn’t able to find a

  • 0

I realize this has been asked before, but I wasn’t able to find a question that really dealt with what I’m trying to do. I think it’s pretty simple, but I’d like to know what the general population thinks is best form here.

Lets say we have the following:

models.py

class TestClass(models.Model):
    user = models.ForeignKey(User)
    testfield = models.CharField()
    testbool = models.BooleanField(default=False)

def save(self, *args, **kwargs):
    """
    - what we're trying to do here is ensure that the User doesn't have more than
      X (lets say 5) related test fields.
    - what if we also wanted to add validation to testfield to ensure it was 
      only [a-zA-Z]?
    """
    if TestClass.objects.filter(user=self.user).count() >= 5:
        # How do we exit gracefully?
        return 
    super(TestClass, self).save(*args, **kwargs)

The comments in the save function pretty much sum up my question:
– How would we ensure that there aren’t more than 5 related TestClass’s to the giving user
– How do we exit gracefully from save (without saving) if there are already 5
– How do we report this back to the user?
– where do we validate the testfield object to ensure it only has [a-z]? can I just import re and do that here as well? should i?

Is it best to throw this all in here? Should I fire off a pre_save signal? Or should I just use a ModelForm w/ validation?

  • 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-11T21:24:39+00:00Added an answer on May 11, 2026 at 9:24 pm

    You’re right that there’s currently no obvious place to do this. That’s why there’s a Summer of Code project going on right now to add model-level validation, which should make this sort of thing nice and easy.

    Unfortunately it won’t be ready for a couple of months. In the meantime, your answer depends on how data is being added. Will it ever be done programatically, or will it always be via a form? If the latter, then putting the validation into the form is the way to go (note that you can use the same modelform, or subclasses of it, in both the admin and your own view, so no need to duplicate logic).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realize that this question has been asked before - but none of the
I realize that this type of question has been asked several times before but
I realize that this question has been asked 100times but none that I have
While I realize that this question has been asked once or twice ago but
I realize this question has been asked before, but I'm at my wit's end
I realize this question has been asked before, but I can't get it to
I realize that this question has been asked and has been answered here but
I realize that this question has been asked many times, but I've yet to
I realize this question has probably been asked numerous times, but I have not
I realize that a SO user has formerly asked this question but it was

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.