I have two models one is user model and another one is cars model.
class User(models.Model):
id = models.BigAutoField(primary_key=True)
username = models.CharField(max_length=50, verbose_name="User Name")
class Cars(models.Model):
id = models.BigAutoField(primary_key=True)
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='cars')
Now on /admin/app/cars/1/change/
page i want to see the username as a normal text in that form below the dropdown of users.
I tried with inline stack but that didn't worked, and throwing error, User
has to foreign key to Cars
.
CodePudding user response:
The text shown in the admin panel is determined by the __str__
function, so adding something like
def __str__(self):
return self.username
to your user model should be enough
CodePudding user response:
do this
class Cars(models.Model):
id = models.BigAutoField(primary_key=True)
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='cars')
def __str__(self):
return self.user.username