Pregunta

I have the following code:

class Item(models.Model):
    name = models.CharField(max_length=100)
    keywords = models.CharField(max_length=255)
    type = models.ForeignKey(Type)

    class Meta:
        abstract = True

class Variant(models.Model):
    test_field = models.CharField(max_length=255)

class Product(Item):
    price = models.DecimalField(decimal_places=2, max_digits=8,null=True, blank=True)
    brand = models.ForeignKey(Brand)
    variant = models.ForeignKey(Variant)

    def get_fields(self):
        return [(field.name, field.value_to_string(self)) for field in Product._meta.fields]

    def __unicode__(self):
        return self.name

Im using Grappelli. I want my Product to have multiple Variations. Should I use a manytomanyfield?

I want to be able to add Variants to my Product directly in the Admin. Now I get an empty dropwdown with no variants(because they doesnt exists).

I thought Django did this automatically when u specified a Foreign Key?

How can I get the Variant fields to display directly on my Product page in edit? I've read someting about inline fields in Admin?

¿Fue útil?

Solución

Well, it's the other way around :)

1/ Place the foreign key field in your Variant, not in your Product (what you describe is actually a OneToMany relationship).

2/ Link the Variant to your Product in the relative ProductAdmin in admin.py as an inline (i.e VariantInline).

See the docs for further informations : https://docs.djangoproject.com/en/1.6/ref/contrib/admin/#inlinemodeladmin-objects

Hope this helps !

Regards,

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top