Question

I am having the following custom user model trying to use the Django 1.5 AbstractBaseUser:

class Merchant(AbstractBaseUser): 
    email = models.EmailField()
    company_name = models.CharField(max_length=256)
    website = models.URLField()
    description = models.TextField(blank=True)
    api_key = models.CharField(blank=True, max_length=256, primary_key=True)   

    USERNAME_FIELD = 'email' 
    REQUIRED_FIELDS = ['email','website']


   class Meta:
        verbose_name = _('Merchant')
        verbose_name_plural = _('Merchants')

   def __unicode__(self):
        return self.company_name 

The model works perfectly and database is as expected, but the problem is when I try to dumpdata to create fixtures for my tests.

python manage.py dumpdata --natural --exclude=contenttypes --exclude=auth.permission --indent=4 > fixtures/initial_data.json

Then I get the error:

CommandError: Unable to serialize database: <Merchant: Test Shop> is not JSON serializable

Do you have ideas what could be the reason for this. Could it be the charfield primary key or something with the abstractbaseuser model?

Thanks

Was it helpful?

Solution

After some time spend I found the problem. Actually it was not in Merchant model but in Product that has foreign key to Merchant:

class Product(models.Model):
    name = models.CharField(max_length=200)
    #merchant = models.ForeignKey(Merchant, to_field='api_key')
    merchant = models.ForeignKey(Merchant)
    url = models.URLField(max_length = 2000)  
    description = models.TextField(blank=True) 
    client_product_id = models.CharField(max_length='100')

    objects = ProductManager() 
    class Meta:
        verbose_name = 'Product'
        verbose_name_plural = 'Products' 
        unique_together = ('merchant', 'client_product_id',) 

    def __unicode__(self):
        return self.name 

    def natural_key(self):
        return (self.merchant, self.client_product_id) 

the natural_key method in the model returned self.merchant instead of self.merchant_id so it was trying to serialize the whole merchant object to create a natural key. After switching the natural_key method to the following one the problem was fixed:

def natural_key(self):
    return (self.merchant_id, self.client_product_id)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top