python - Display row count from another table in Django -
I want to count on the number of membership on a particular hardware node and show that 10 memberships hosted on node 2 on the admin section for HardwareNode class. I am still learning about the Django and I am not sure how to complete it. Can I do it in models.py or in HTML? Thank you, -seth
a When creating foreign_key
, another model gets a manager who gives all examples of the first model (see) in your case, it will be named "" subscription_set
".
In addition, the Django model allows for virtual fields, called "model methods", which are not connected to database data, but these models (see)
By putting all together, you can do something like this:
class hardware node (models.Model): ip_address = models.CharField (max_length = 15) Port = models.IntegerField () Location = models.CharField (max_length = 50) hostname = models.CharField (max_length = 30) subscription_count = lambda (self: self.subscription_set.count ())
And then, Listed in the admin panel Include subscription_count of th Fields made.
Note: As always, I did not check this code, and this can not be as much as it is, but it should give some thought about working on your problem; Apart from this, I have used a lambda for short but generally I think it would be a better option (style, maintenance, etc.) to use a designated one.
Comments
Post a Comment