Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

python - Foreign key of models should refer to a particular filed in db - Django

class UserModel(AbstractUser):
   USERNAME_FIELD = 'username'
   REQUIRED_FIELDS = ['email', 'password']
   email = models.EmailField(unique=True)
   profile_image = models.ImageField(upload_to='user/profile_image', null=True, blank=True)
   phone = models.CharField(max_length=12, unique=True, blank=True, null=True)
   gender = models.CharField(max_length=10, choices=GENDER_CHOICES)
   emp_id = models.CharField(max_length=20, unique=True, blank=True, null=True)

class IndividualWorkAnalysis(models.Model):
   user_id = models.ForeignKey(UserModel, to_field="emp_id", on_delete=models.CASCADE, null=True,
                            related_name="user_id")

Here above I need to access 'emp_id' from 'UserModel'.

All my migrations went correct, but not able to fetch the 'emp_id' on the admin panel of Django.

There it just lists the usernames.

question from:https://stackoverflow.com/questions/65838763/foreign-key-of-models-should-refer-to-a-particular-filed-in-db-django

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...