milan
milan

Reputation: 2417

update preview field after submitting the value

I have a model for storing document numbering where the fields to fill are document, prefix, suffix, length and start. This I could do but after filling those data I need to show preview field in the table and preview field is based on the prefix, suffix and length.

What I mean to say is if I fill the prefix as 1, suffix as -2345 and length as 4 then the preview should be for that document as

1xxxx-2345

if prefix as he-, suffix as -7459 and length as 5 then the preview should be

he-xxxxx-7459

This can be done with the following logic

preview = str(prefix)+(('x')*length)+str(sufix)

But how can I implement this in django. Do I have to use signals or override save method? Can anyone give me an idea, please?

Here is my model

class DocumentNumbering(models.Model):
    office = models.OneToOneField(OfficeSetup, blank=True, null=True)
    document = models.OneToOneField(Document blank=False, null=False)
    prefix = models.CharField(max_length=100, blank=True, null=True)
    sufix = models.IntegerField(blank=True, null=True)
    start_number = models.PositiveIntegerField(
        default=0, blank=False, null=False)
    length = models.PositiveIntegerField(default=0, blank=False, null=False, validators=[
                                         MaxValueValidator(10), MinValueValidator(1)])

    // not sure where to implement the logic for preview
    def save(self, *args, **kwargs):

Upvotes: 1

Views: 33

Answers (1)

VMatić
VMatić

Reputation: 996

Do you really need to store this in the database? If it is just for display purposes you could use a derived property:

@property
def preview(self):
    return str(self.prefix)+(('x')*self.length)+str(self.sufix)

Upvotes: 1

Related Questions