0
votes

I'm new to django and I have a web application, in which I have an model called Template. this model has one field called body and one field called tokens. sample body would be : "I am {{ first_name }} {{ last_name }}" I want tokens field to be filled by comma separated values : "first_name, last_name" so in admin form for creation and change I don't want to be asked for this field value. how can I achieve this in django? here is what I tried, which doesn't work.

class SmsTemplate(models.Model):
    """ Message SMS Template """
    name = models.CharField(max_length=255)
    body = models.TextField()
    tokens = models.SlugField()
    created = models.DateTimeField(auto_now_add=True)

    def save(self, *args, **kwargs):
        if self.pk is None:
            self.tokens = ",".join(re.findall(r'{{\s*(.*?)\s*}}', self.body))
        super().save(*args, **kwargs)

python 3.7.0

django 2.1.1

1

1 Answers

1
votes

You should remove field from django admin page . There is an attribute fields in admin.ModelAdmin class. It will not show this field in admin page. Moreover, in save method of model you can remove if statement.