python - Have custom manager but keep objects keyword available -
i learning python + django (basically in love - 100% .net guy before), , trying learn concept of managers.
i trying add custom manager called providermanager
, objects depending on input.
however, still want able stuff like:
city= city.objects.filter(id = city_id).first()
inside views.py
file.
i understand when add new custom manager, override objects keyword.
however, question is: how both have objects available, new provider method, can city.provider.get_provider()
?
current code:
class providermanager(models.manager): def get_provider(country, city=0): return "abekat" class city(models.model): title = models.charfield(max_length=200) country = models.foreignkey(country, null=true) is_visible = models.booleanfield(default=false) providers = providermanager() def __str__(self): return self.title
views.py:
def city(request, country_id, city_id): country= country.objects.filter(id = country_id).first() city= city.objects.filter(id = city_id).first() print city data = { 'country': country.title, 'city': city.title, 'provider': 'abe', } return render(request, 'city.html', data)
managers provide methods table-wide operations (referring table of model added to). if want write manager city model best pratice call citymanager.
since custom managers inherit models.manager, can overwrite objects name in model class. won't change behaviour of standard objects manager, custom methods available under objects name.
see documentation more details: https://docs.djangoproject.com/en/1.8/topics/db/managers/#custom-managers
and please pay attention distinction between managers , model methods,latter ones "row-level" functionality explainded in documentation.
Comments
Post a Comment