0

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)

2 Answers 2

1

Neither can be done at the ORM level; you will need to add database-level constrains using raw SQL.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.