Reputation: 31
1.Kindly tell how can we set the limit for integer fields in django .
2.Kindly tell the codings for the block name (Add Another Choice) should be of limited for the program
class Record(models.Model):
Name = models.CharField(max_length=200,blank=True,null=True,help_text="Employee Name")
Empid = models.CharField(max_length=300,blank=True,help_text="Employee ID")
Salary = models.CharField(max_length=300,blank=True,null=True)
Bonus = models.IntegerField(blank=True,null=True)
class Choice(models.Model):
p=models.ForeignKey(Record)
Month=models.CharField(max_length=200,blank=True,null=True)
Upvotes: 0
Views: 89
Reputation: 22459
1 If you just want to check user input use a clean method
ie.
def clean_bonus(self): #thus: clean_FIELDNAME
#check the value and show an error
2 I am not sure what you mean:
a) If you mean you want users to select atleast 1 choice, but limit the maximum use something like this:
class RequireOneFormSet(BaseInlineFormSet):
"""
Require at least one form in the formset to be completed.
"""
def clean(self):
"""Check that at least one form has been completed."""
super(RequireOneFormSet, self).clean()
for error in self.errors:
if error:
return
completed = 0
for cleaned_data in self.cleaned_data:
# form has data and we aren't deleting it.
if cleaned_data and not cleaned_data.get('DELETE', False):
completed += 1
if completed < 1:
raise forms.ValidationError("At least one %s is required." %
self.model._meta.object_name.lower())
b) If you mean you want to have multiple foreign keys use django ContentTypes
Upvotes: 0
Reputation: 799300
Neither can be done at the ORM level; you will need to add database-level constrains using raw SQL.
Upvotes: 1