Aram.D
Aram.D

Reputation: 5

python - django - using a flag on each model field

I want to build a simple moderation system for my application.I have a class in my application models like this:

#models.py
class TableName(models.Model):
        is_qualified = False
        title = models.CharField(max_length=300, blank=False)
        description = models.TextField(max_length=500, default="DEFAULT VALUE")  
        video = models.FileField(upload_to='somepath')
        picture_thumbnail = models.ImageField(upload_to='somepath')

I have 3 questions:

  1. How can I add is_qualified to every field in my model and setting it to False as default?
  2. How can I write a view method first for checking if admin checked an object (for example title or description) and used its checkbox to change field's is_qualified value to True?
  3. How to add a checkbox for each object in admin area for using that view method?

Thank you very much.

Upvotes: 0

Views: 2989

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599788

You need to make is_qualified an actual field - a BooleanField would be appropriate - and have it default to False.

is_qualified = models.BooleanField(default=False)

You can also look at documentation here

Upvotes: 1

sebb
sebb

Reputation: 1966

Hmm, adding is_qualified for each field would be a bit too much.

If your are using postgresql I would consider using django-hstore, where you can dynamically add key-value fields.

Using this package, you can make something like your field name as a key, and True/False as a value.

Then when trying to validate is your object "qualified" you just make something like this:

is_valid = all([value for key, value in your_hstore_field.items()])


EDIT

class TableName(models.Model):
        is_qualified = models.BooleanField(default=False)
        title = models.CharField(max_length=300, blank=False)
        description = models.TextField(max_length=500, default="DEFAULT VALUE")  
        video = models.FileField(upload_to='somepath')
        picture_thumbnail = models.ImageField(upload_to='somepath')
        data = hstore.DictionaryField()

Then you can have some custom function like this:

def update_qualified(obj_instance):
    if all(value for key, value in obj_instance.data.items()):
        obj_instance.is_qualified = True
    else:
        obj_instance.is_qualified = False
    obj_instance.save()

Upvotes: 2

Related Questions