2016-04-13 23 views
0

I Unter der Annahme hat:ManyToManyFields als IntegerFields in Django Admin-Interface

class Product(models.Model): 
    [...] 

class Basket(models.Model): 
    content = models.ManyToManyField(Product, through="ProductQuantity") 

class ProductQuantity(models.Model): 
    basket = models.ForeignKey(Basket) 
    product = models.ForeignKey(Product) 
    quantity = models.IntegerField(default=0) 

Wie könnte ich ein ModelForm für das Basket Modell mit einem Feld für jeden ProductQuantity ein Basket, nur um in die Lage zu ändern, um ihre quantity Attribute machen ?

Gibt es ein Widget, das ich dafür verwenden könnte?

Wenn ich war in der Lage so etwas mit einer solchen Modelform zu tun, konnte ich diese ModelForm in einem admin.ModelAdmin als Alternative form Attribut verwenden das gleiche Verhalten im Admin-Interface haben?

Edit:

@MuhammadTahir markiert diesen Beitrag wie möglich Duplikat this post.

es in der Tat hat mir geholfen, besser zu verstehen, aber ich bin immer noch fest: ich die Felder nicht machen kann ich machen möchte.

Hier ist mein Code so weit:

models.py

Wie oben.

forms.py

ProductQuantityFormSet = inlineformset_factory(Product, 
               basket.content.through, 
               fields=("quantity",)) 

admin.py

class ProductQuantityInline(admin.StackedInline): 
    model = ProductQuantity 
    formset = ProductQuantityFormSet() 

class BasketAdmin(admin.ModelAdmin): 
    inline = [ProductQuantityInline,] 
+1

Mögliche Duplikat [Zugriff auf viele zu viele „durch“ Beziehung Felder in Formsets] (http://stackoverflow.com/questions/11021242/accessing-many-to-many-through-relation-fields-in- Formsätze) –

Antwort

0

Jemand im IRC fand das Problem:

inlineinlines im admin.ModelAdmin. Und ich brauche nicht einmal die inlineformset_factory().

Also hier ist der letzte Code:

models.py

class Product(models.Model): 
    [...] 

class Basket(models.Model): 
    content = models.ManyToManyField(Product, through="ProductQuantity") 

class ProductQuantity(models.Model): 
    basket = models.ForeignKey(Basket) 
    product = models.ForeignKey(Product) 
    quantity = models.IntegerField(default=0) 

admin.py

class ProductQuantityInline(admin.StackedInline): 
    model = ProductQuantity 
    fields = ["quantity",] 

class BasketAdmin(admin.ModelAdmin): 
    inlines = [ProductQuantityInline,] 

Ich hoffe, das jemand anderes helfen könnte. :)