[Answered ]-Auto-populate Django Model field

2👍

You don’t need 2 fields in Order. In Order model you need this:

status = models.ForeignKey(Status, related_name='+')

And you can access name and value by order.status.name and order.status.value.

0👍

For particular case you don’t need two models. You can do it by single model using choice field https://docs.djangoproject.com/en/1.8/ref/models/fields/#django.db.models.Field.choices

class Status(models.Model):

STATUS_PENDING = 0
STATUS_COMPLETE = 1
STATUS = (
    (STATUS_PENDING, 'Pending'),
    (STATUS_COMPLETE, 'Complete'),
name = models.CharField(max_length=30, blank=True)
value = models.DecimalField(max_digits=10, decimal_places=2, default=Decimal(0.0))
status = models.IntegerField(default=0, choices=STATUS)

Leave a comment