Wendy
Wendy

Reputation: 1603

Django admin doesnot reflect dynmaic choices in choiceField

I have 2 projects running on 2 different systems.(call them A and B)

in A I have a model which has one dynamic choice field.

class ModelA(models.Model):
      field1 = models.CharField(max_length=255, choices=get_field1_list())
      #..some more fields

and in get_field1_list() function I call an API which is running on system B and it returns a list of tuples(required in choice field) which is coming from ModelB (Project B)

[(a,A), (b,B), (c,C)...]

Now when I make changes in this model(ModelB) from admin panel(lets say added one more row) so I was expecting that it should reflect in the modelA choices. I refreshed the admin panel but still it doen't show. but when I restart server A(local server), I can see newly added(in system B) row in ModelA(System A) choices.

So My question is why this happening? how can I resolve this issue as in production I might not want to restart uwsgi or nginx everytime.

Upvotes: 0

Views: 78

Answers (1)

Gaurav Jain
Gaurav Jain

Reputation: 1865

In Django 1.9, I'm doing this way

from django.utils.functional import lazy

class ModelA(models.Model):

    field1 = models.CharField(max_length=255, blank=False, null=False)
    # ..

    def __init__(self, *args, **kwargs):
        super(ModelA, self).__init__(*args, **kwargs)
        self._meta.get_field('field1').choices = lazy(get_field1_list, list)()

NOTE that i'm using lazy. you can ignore that

Upvotes: 1

Related Questions