python - Where do I put a function to count foreign keys in Django? -
i have django model object, record, has foreign keys 2 other models recordtype , source:
class record(models.model): title = models.charfield(max_length=200) record_type = models.foreignkey(recordtype) source = models.foreignkey(source)
question: if want count number of record objects refer recordtype id "x" , source id "y", appropriate area of code put function?
right have in views.py , feel violation of best practices "fat model, thin views", want move views.py. i'm not entirely sure if row-based or table-based type of operation, i'm not sure if should implemented model method, or instead manager.
here's current (working) logic in views.py:
record_count = record.objects.filter(record_type__id=record_type_.id, source__id=source_.id).count()
just clear, isn't question of how count, in area of code put function.
here's similar question, addressing "how to" not "where": counting , summing values of records, filtered dictionary of foreign keys in django
if result involves multiple rows, table-related method, , according django conventions, should manager method.
from the django docs:
adding manager methods preferred way add “table-level” functionality models. (for “row-level” functionality – i.e., functions act on single instance of model object – use model methods, not custom manager methods.)
Comments
Post a Comment