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

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you running on OS 3.0? I saw the same… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer It looks like you need to register Apache::Session::Memcached with Apache::Session::Wrapper,… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer Use DATENAME or DATEPART: SELECT DATENAME(dw,GETDATE()) -- Friday SELECT DATEPART(dw,GETDATE())… May 12, 2026 at 1:19 am

Related Questions

I realize this has been asked before, but I wasn't able to find a
I realize this question is very likely to have been asked before, but I've
I realize that similar questions have been asked about this before here on SO,
I'm sure this has been asked before, but I can't find it. What are
This is a question which has been asked before ( large-text-and-images-in-sql ) but mainly

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.