Question

Je veux étendre ForeignKeyRawIdWidget donc je veux pouvoir l'utiliser sans réglage raw_id_fields.

Avec le follwoing je ne reçois pas une erreur, mais je ne vois pas d'effet:

# models.py
class Product(models.Model):
    ...

class GroupProduct(Product):
    ...
    products = models.ManyToManyField(Product, related_name="%(class)s_related")

# forms.py
class GroupProductAdminForm(forms.ModelForm):    
    class Meta:
        model = GroupProduct
        widgets = {
            'products': ForeignKeyRawIdWidget(GroupProduct._meta.get_field('products').rel),
        }

Cela me donne une erreur: init () prend au moins 2 arguments non de mots clés (1 donné)

products = forms.ModelMultipleChoiceField(widget=ForeignKeyRawIdWidget(GroupProduct._meta.get_field('products').rel))

Comment puis-je faire?

Merci

Était-ce utile?

La solution 2

Utilisation ManyToManyRawIdWidget au lieu de ForeignKeyRawIdWidget il fixe pour moi.

Autres conseils

Vous avez oublié de passer le modèle QuerySet à ModelMultipleChoiceField liées.

products = forms.ModelMultipleChoiceField(Product.objects, widget=ForeignKeyRawIdWidget(GroupProduct._meta.get_field('products').rel))
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top