python - Django model field dependent on instance -


i trying create model field dependent on instance (specifically instance of foreign key).

example:

class user(models.model):     name = models.charfield()     ...  class entry(models.model):     user = models.foreignkey(user)     file = models.filefield(upload_to=user.name) 

as can see, want file "upload_to" parameter dependent on user. there easy way this?

upload_to may callable, such function, called obtain upload path, including filename.

this callable must able accept 2 arguments, , return unix-style path (with forward slashes) passed along storage system.

def _upload_to(instance, filename):     return str(instance.user.name)  class entry(models.model):     user = models.foreignkey(user)     file = models.filefield(upload_to=_upload_to) 

Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -