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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:45:12+00:00 2026-05-26T15:45:12+00:00

I have a strange need, a customer asked me to implement a webapp to

  • 0

I have a strange need, a customer asked me to implement a webapp to support his database. Since I love Django I decided to use it.

The extra need which I have difficult with is as follow: the user should have the extra “power” to create model constraints like checking the range of an IntegerField, all without knowing anything about Python.

So, I have some ideas but I’m not sure what is (if there is) the correct one. Here’s my thoughts:

  1. Implement a template-side validation using JavaScript
  2. Implement a Python Script and a userfriendly way to use it that writes the models.py file (this sounds terrible)
  3. Write some Admin Actions using a raw SQL CHECK constraint, but I still don’t know how

Is this request solvable? Can you give me a tip about the simplest way to do it?

Thanks in advance,

—
Flavio

  • 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-26T15:45:12+00:00Added an answer on May 26, 2026 at 3:45 pm

    Yes, it is possible indeed. We will use custom object manager and dynamic Q objects to solve the issue. Here’s an example.

    1. Create an appropriate model to store info about constraints. Note that we create ForeginKey to ContentType in order to tie our constraint to a specific model class.

      #your_app/models.py
      from django.contrib.contenttypes.models import ContentType
      from django.contrib.auth.models import User
      
      class PerUserConstraint(models.Model):
          user = models.ForeignKey(User)
          content_type = models.ForeignKey(ContentType)
          field_name = models.CharField(max_length = 255)
          predicate = models.CharField(max_length = 255) #for example, 'contains' etc.
          filter_value = models.CharField(max_length = 255)
      
    2. Create a custom manager, register it with your models.

       #your_app/models.py
       from django.db.models.query_utils import Q
      
       class ConstraintedManager(models.Manager):
           def get_constrainted_query(self, user):
               qs = self.model.objects.all()
               content_type = ContentType.objects.get_for_model(self.model.__class__)
               constraints = PerUserConstraint.objects.filter(
                                                    user = user,
                                                    content_type = content_type
               )
               if constraints:
                   filters = []
                   for constraint in constraints:
                       filters.append(('%s__%s' % (constraint.field_name, constraint.predicate), constraint.value)
                   # at this point filters should look like this:
                   # [('question__contains', 'dinner'), ('question__contains', 'meal')]
                   q_list = [Q(x) for x in filters]
                   return qs.filter(reduce(operator.and_, q_list))
                   # which is something like 
                   # return qs.filter(Q(question__contains = 'dinner') & Q(question__contains = 'meal'))
      
              else:
                  return qs     
      
       class YourModel(models.Model):
           #some fields
           ... 
           c = ConstraintedManager()
      

    So in your views you will be able to use it like this: YourModel.c.get_constrainted_query(user)

    Note that I didn’t test this code, but I hope you’ve grasped the concept.

    To override queries inside ModelAdmin, use this and this techniques.

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

Sidebar

Related Questions

Friends, I have a strange need and cannot think my way through the problem.
I have a strange (don't ask) need to see a few examples of a
I have a strange application in that for development and testing I need a
After upgrading to Django 1.2 I have strange urls in my administration panel. They
I'm making an Android app and have a strange problem. I need a ViewFlipper
I have a strange space character in that somehow got inserted into my database.
I have a strange need. I would like to do the following in javascript:
I have a strange case that (I think) I need two resource IDs for
I have a strange question. I need to send some code to a client
Im using latest TCPDF version(5.9). But have some strange problems with encoding. I need

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.