how can i set a model’s field using the existance of a foreignkey as a condition?

You are on correct path. upload_to can point to a function.

def user_directory_path(instance: Person, filename): 
    # file will be uploaded to MEDIA_ROOT / your path 
    if instance.team:  # 
        return f"team_{instance.team.id}/user_{instance.id}"
    else:
        return f"person_{instance.id}"

# ...
class Person(models.Model): 
    files = models.FileField(upload_to = user_directory_path) 

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top