1

голосов
1

ответ
151

Просмотры

Django Generic Factory

I have two questions that bother me. I'm facing an implementation where some document are related to different level of geodata and would like a factory to generate them. Let's see an example how I thought it may work: from django.contrib.gis.db import models class Country(models.Model): name = models.CharField(max_length=60) class Region(models.Model): country = models.ForeignKey(Country, on_delete=models.PROTECT) name = models.CharField(max_length=60) class Law(models.Model): text = models.CharField(max_length=60) class Meta: abstract = True class CountryLaw(Law): country = models.ForeignKey(Country, on_delete=models.CASCADE) class RegionLaw(Law): region = models.ForeignKey(Region, on_delete=models.CASCADE) # Not sure this work but the idea is here class LawManager(models.Manager): def create_law(text,geodata): if isinstance(geodata, Country): return CountryLaw(text=text, country=geodata) elif isinstance(geodata, Region) return RegionLaw(text=text, region=geodata) else: raise TypeError("Inapropriate geodata type") I would like some factory method because I have some work to fill the fields of the "Law" that is common to all law, but this example doesn't show it. My question are the following : Is there a better way to design Law objects ? Would such a manager work ? How could I access it ? I search on google and stackoverflow for answer, but don't know what keyword to use and didnt found anything that could help me.. thanks for your help !
Merichbier