python - Incorporate third-party module into Django models -
i'm creating product id conversion app. have 2 models representing 2 id styles:
class id1(models.model): number = models.charfield(max_length=10) converted = models.charfield(max_length=13) status = models.charfield(max_length=5) # validation status error = models.charfield(max_length=10) # error message def __unicode__(self): return self.number class id2(models.model): number = models.charfield(max_length=13) converted = models.charfield(max_length=10) status = models.charfield(max_length=5) # validation status error = models.charfield(max_length=10) # error message def __unicode__(self): return self.number
there third-party python (non-django) module i'd incorporate django models 2 reasons:
- it provides conversion functionality need
- i want use oo approach in doing conversion - create instance of class (e.g. id1) encapsulates relevant data , methods instance.
the third-party module has following classes several fields , methods each:
- class id(object)
- class id1(id)
- class id2(id)
... , following top-level functions class methods invoke:
- def validate
- def convert
- def calculate_checksum
- def cleanse
however, don't want third-party module's attributes in database; each database table should contain django model fields (number , converted).
how can achieved? if isn't possible, alternative ways approach problem? delegate logic view?
[edit] forgot add i'd use module methods initialize of fields "converted", "status", , "error". idea how this?
well, there might several approaches:
first, can import module , encapsulate relevant logic inside custom methods inside model object this:
import module class id1(models.model): ... def custom_validate_method(self): # instantiate modules classes needed # write logic needed
then able call method instance of id1
class. or may override save()
method perform logic when try persist object this:
def custom_validate_method(self): # instantiate modules classes needed # write logic needed def save(self, *args, **kwargs): # perform logic (maybe call custom_validate_method) return super(id1, self).save(*args, **kwargs)
you may use multiple inheritance achieve watch it, multiple inheritance can bring name conflicts. this:
import module class id1(models.model, module.class): number = models.charfield(max_length=10) converted = models.charfield(max_length=13) def __unicode__(self): return self.number
there have functionality of class need in module inherited in id1 class.
hope helps.
Comments
Post a Comment