python - Automate some entries in Django models passing the entry value from same model -


this models.py code:

from django.db import models import shortuuid  class website(models.model):     url = models.urlfield(max_length=100)     uid = models.charfield(unique = true, max_length=40,default=str(shortuuid.uuid(name=url)))  def __unicode__(self):     return self.url 

in django admin panel value of uid not change when enter url.i want enter url , want generate uid using shortuuid function.i want set uid editable=false before want ensure function woking properly.how automate uid value passing url input?

that's not it. can't write @ class level depends on instance attribute of class: it's not possible. , what's more, default allocated when object instantiated, want change after user has changed value of attribute, isn't default @ all.

instead want define value on save. that's easy overriding save method:

def save(self, *args, **kwargs):     if not self.uid:         self.uid = str(shortuuid.uuid(name=self.user))     return super(website, self).save(*args, **kwargs) 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -