0

I want to use a foreign key in limit_choices_to, but i am getting an error of

Cannot resolve keyword u'product_type.product_type' into field. Choices are: id, pantvariation, product_brand, product_brand_id, product_id, product_type, product_type_id, shirtvariation

class ProductType(models.Model):
    product_type = models.CharField(max_length=70, choices=choices.PRODUCT_TYPES)
    variation_model_name = models.CharField(max_length=70,blank=True, null=True)

    def save(self, *args, **kwargs):
        temp = ProductType()
        temp.product_type = self.product_type
        temp.variation_model_name = self.product_type.capitalize() + "Variation"
        super(ProductType, temp).save(*args, **kwargs)

    def __unicode__(self):
        return self.product_type


class WarehouseStock(models.Model):
    product_id = models.CharField(max_length=120, unique=True)
    product_type = models.ForeignKey(ProductType)
    product_brand = models.ForeignKey(Brand)

    def __unicode__(self):
    return self.product_id
class PantVariation(models.Model):
    product = models.ForeignKey(WarehouseStock, limit_choices_to= {'product_type.product_type': 'pant'})
    size = models.CharField(max_length=3, choices= choices.PANT_SIZES)
    colour = models.CharField(max_length=10)
    fit = models.CharField(max_length=10, choices=choices.FIT_TYPES)

I want to use product_type.product_type from WarehouseStock in limit_choices_to, how do i do that?

edit: Full error:

FieldError at /admin/warehouse/pantvariation/add/ Cannot resolve keyword u'product_type.product_type' into field. Choices are: id, pantvariation, product_brand, product_brand_id, product_id, product_type, product_type_id, shirtvariation

Request Method: GET

Request URL: http://127.0.0.1:8000/admin/warehouse/pantvariation/add/

Django Version: 1.9.7

Exception Type: FieldError

Exception Value: Cannot resolve keyword u'product_type.product_type' into field. Choices are: id, pantvariation, product_brand, product_brand_id, product_id, product_type, product_type_id, shirtvariation

Osiris92
  • 181
  • 8

1 Answers1

0

IMHO you cannot make this work easily that way. However, I would restructure your PantVariation model in this way:

class PantVariation(models.Model):
    product = models.ForeignKey(ProductType, related_name="pants", limit_choices_to= {'product_type.': 'pant'}

)

That should work. And you still can access the WarehouseStock model via the ProductType model. Does this do what you want?

ger.s.brett
  • 3,267
  • 2
  • 24
  • 30
  • No, unfortunately it doesn't, I want the foriegnkey to be to WarehouseStock so i only see products which are pants. Making the foriegn key to ProductType will let me choose betweeen the product_type not the product_id that has pant type. – Osiris92 Jul 02 '16 at 05:33
  • You can take a look at this answer: http://stackoverflow.com/questions/232435/how-do-i-restrict-foreign-keys-choices-to-related-objects-only-in-django ? – ger.s.brett Jul 02 '16 at 08:14