Incorporate third-party module into Django models

I'm creating a product ID conversion app. I have two models representing the two 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 is a third-party Python (non-Django) module that I'd like to incorporate into the Django models for two reasons:

  1. It already provides the conversion functionality I need
  2. I want to use an OO approach in doing the conversion - I create an instance of a class (e.g. Id1) that encapsulates the relevant data and methods for that instance.

The third-party module has the following classes with several fields and methods each:

  • class Id(object)
  • class Id1(Id)
  • class Id2(Id)

... and the following top-level functions which the class methods invoke:

  • def validate
  • def convert
  • def calculate_checksum
  • def cleanse

However, I don't want the third-party module's attributes in the database; each database table should only contain the Django model fields (number and converted).

How can this be achieved? If this isn't possible, what are some alternative ways to approach this problem? Delegate logic to the view?

[EDIT] I forgot to add that I'd like to use the module methods to initialize some of the fields like "converted", "status", and "error". Any idea how to do this?

Answers


Well, there might be several approaches:

First, you can import the module and encapsulate their relevant logic inside custom methods inside your model object like this:

import module
class Id1(models.Model):

    ...

    def custom_validate_method(self):
        # instantiate modules classes needed
        # write the logic needed

Then you will be able to call this method with any instance of Id1 class. Or you may as well override the save() method to perform some extra logic when you try to persist the object like this:

    def custom_validate_method(self):
        # instantiate modules classes needed
        # write the logic needed

    def save(self, *args, **kwargs):
        # perform logic (maybe call custom_validate_method)
        return super(Id1, self).save(*args, **kwargs)

You may also use multiple inheritance to achieve this but watch it, multiple inheritance can bring some name conflicts. It would look like 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 you have the functionality of the class you need in the module inherited in your Id1 class.

Hope it helps.


Need Your Help

Unable to load store data in EXTJS Simplestore using JavaScript

javascript extjs

I am trying to re-load data into the Store which is further used by a GridPanel. My code goes like:

$(this).hide(); doesn't close the popup div no matter what. jQuery

jquery html css popup hide

I'm struggling to close a popup window, which is shown on a click, but it doesn't work for some reason that is not obvious to me.