I am very new to Django and I was wondering if I could request some help with an issue I am facing. I'm trying to build a set of models in Django that is structured as follows:
An app_user
describes a user of the application I am building.
An app_job
describes a job that the user wants to run on the app, with several associated inputs (upload1
, upload2
, upload3
). A user can run many jobs; hence, I use the many-to-one (ForeignKey
) relationship between job and user. When an app_job
is created, I want its associated files to be uploaded to a directory determined by the associated user's username
and num_jobs
attribute, as shown.
When I run python manage.py makemigrations
, I receive the following error: AttributeError: 'ForeignKey' object has no attribute 'user'
. Which begs the question, how can I access the underlying app_user
's information from the app_job
class?
Thanks for the help.
# models.py
from django.db import models
from django.contrib.auth.models import User
from django.forms import ModelForm
class app_user(models.Model):
user = models.OneToOneField(User)
num_jobs = models.IntegerField(default = 0)
def __unicode__(self):
return self.user.get_username()
class app_job(models.Model):
app_user = models.ForeignKey(app_user)
upload1 = models.FileField(upload_to = app_user.user.get_username() + "/" + str(app_user.num_jobs) , blank = True, null = True)
upload2 = models.FileField(upload_to = app_user.user.get_username() + "/" + str(app_user.num_jobs))
upload3 = models.FileField(upload_to = app_user.user.get_username() + "/" + str(app_user.num_jobs) )