简体   繁体   中英

Django accessing ForeignKey model objects

Let's say I have the following:

class Employee(models.Model):
    firstName = models.CharField(max_length = 30)
    lastName = models.CharField(max_length = 30)

class License(models.Model):
    employee = models.ForeignKey(Employee)
    type = models.CharField(max_length = 30)

and in a custom management command, I'm pulling in all the Employee objects with employees = Employee.objects.all() ... how can I access associated License objects for each employee object? I have seen questions that talk about using ContentType for this purpose, but I'm confused on it's practical use. Can anyone provide an example on how to use ContentType (or another solution) in this context?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM