python - Display row count from another table in Django -


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) def __unicode __ (self): return self.hostname class membership (Models.Model): Customer = Model. Exotic (customer) package = model. Foreign key (package) location = model Forward (hardware node) upgrade_data = model.DateimField ('renewal date') def __unicode __ (self): x = '% S% s'% (self.customer.hostname, str (self.package)) x

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

Popular posts from this blog

c++ - Linux and clipboard -

Visual Studio 2005: How to speed up builds when a VSMDI is open? -

booting ubuntu from usb using virtualbox -