Error in modelformset_factory : matching query does not exist

297 views Asked by At

I have these 3 models:

class Service(models.Model):
    name = models.CharField(max_length=30L, blank=True) 


class ServiceUser(models.Model):
    service = models.ForeignKey(Service, null=False, blank=False)
    contact = models.ForeignKey(Contact, null=False, blank=False) 


class SupplierPrice(models.Model):
    service_user = models.ForeignKey('ServiceUser') 
    price_type = models.IntegerField(choices=PRICE_TYPES) 
    price = models.DecimalField(max_digits=10, decimal_places=4)

I want to create a modelformset_factory with SupplierPrice as model.

The modelformset_factory works perfectly on Service and ServiceUser models. But if I do:

>>> prices = SupplierPrice.objects.filter(service_user = srvuser)

which is a queryset that returns two objects SupplierPrice, and:

>>> SupplierPriceFormSet = modelformset_factory(SupplierPrice)
>>> pricesformset = SupplierPriceFormSet(queryset=prices)
>>> pricesformset.as_p()

It returns a DoesNotExist error : Service matching query does not exist. I must have misunderstood something but what?

1

There are 1 answers

0
Emilio Conte On BEST ANSWER

This exception occurs because of some errors in the referential integrity of my DB. I had to clean it up first in order to have the modelformset_factory working.

Thanks to PeterDeGlopper ! After that, the rendering slowness of the modelform_factory came from an over complex unicode model method.