Question

I have these empty lists of tuples (DOC and COURSE) and when a product is saved, later is added into his specific list

CATEGORY = (
    ('DOC','DOCUMENTAZIONE'),
    ('COURSE','CORSO'),
)

class Product(BaseModel):

    class Meta:
        verbose_name=_('Prodotto')
        verbose_name_plural=_('Prodotti')

    name = models.CharField(_("Nome Prodotto"),max_length=1024, blank = False, null=True)
    category = models.CharField(_("Categoria"),max_length=1024, blank = False, null=True,choices=CATEGORY)


    def __unicode__(self):
        return self.name

DOC = ()
COURSE = ()

try:
    for product in Product.objects.all():
        if product.category == 'DOC':
            DOC = DOC + ((str(product.id), str(product.name.encode('utf-8'))),)
        if product.category == 'COURSE':
            COURSE = COURSE + ((str(product.id), str(product.name.encode('utf-8'))),)
except Exception as e:
    print e
    pass

class ProductOfferDoc(BaseModel):
    class Meta:
        verbose_name = _("Documentazione")
        verbose_name_plural = _("Documentazioni")

    product = models.CharField(max_length=1024, blank=False,null=False, choices=DOC)
    number = models.IntegerField(_('Num.'), default=0, blank=True, null=True)
    price = models.DecimalField(_('Prezzo'),max_digits=10, decimal_places=2,default=0.00,blank=True, null=True )
    offer = models.ForeignKey(Offer, related_name='related_doc')

    def __unicode__(self):
        return self.product

class ProductOfferCourse(BaseModel):

    class Meta:
        verbose_name = _("Corso")
        verbose_name_plural = _("Corsi")

    product = models.CharField(max_length=1024, blank=False,null=False, choices=COURSE)
    number = models.IntegerField(_('Num.'), default=0, blank=True, null=True)
    price = models.DecimalField(_('Prezzo'),max_digits=10, decimal_places=2,default=0.00,blank=True, null=True )
    offer = models.ForeignKey(Offer, related_name='related_course')

    def __unicode__(self):
        return self.product

My problem is that i need to display avaible products, but the new product just added doesn't appear until i restart the server. There is another way to do the same thing? Or exist a function to restart the server automatically when a product is added?

Was it helpful?

Solution

You shouldn't look for a solution to automatically restart your server (that's generally not a good idea!). Instead you should change your code so you don't need to restart the server.

The code populating DOC and COURSE tuples is executed only once because of where you put it. Everything put straight into models.py file (i.e. not living inside functions or classes) is executed exactly once, when Django first loads the apps.

Looking at your code you should probably just replace the product CharFields with ForeignKeys. This way you wouldn't need the DOC and COURSE tuples at all - Django will generate a list of ForeignKey's choices automatically when creating a ModelForm. Keeping ids of other objects inside a CharField is weird and suboptimal for many other reasons anyway.

If you really feel you need to use a dynamic tuple with a list of possible choices (but looking at you code, in this case you really SHOULDN'T!) read answers to this question: Creating a dynamic choice field.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top