Reputation: 5
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:
is_qualified
to every field in my model and setting it to False
as default?is_qualified
value to True
?Thank you very much.
Upvotes: 0
Views: 2989
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
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