Pregunta

I'd like to add multiple dealers support from the dashboard following the oscar's documentation:

You’ll need to enforce creating of a StockRecord with every Product. When a Product is created, Stockrecord.partner gets set to self.request.user.partner (created if necessary), and hence the connection is made

I don't know how to enforce the StockRecord creation. oscar has a dashboard that replaces the django admin, this is an excerpt(first lines) of the view used to create/update a Product:

class ProductCreateUpdateView(generic.UpdateView):
    """
    Dashboard view that bundles both creating and updating single products.
    Supports the permission-based dashboard.
    """

    template_name = 'dashboard/catalogue/product_update.html'
    model = Product
    context_object_name = 'product'

    form_class = ProductForm
    category_formset = ProductCategoryFormSet
    image_formset = ProductImageFormSet
    recommendations_formset = ProductRecommendationFormSet
    stockrecord_formset = StockRecordFormSet

So the Product creation view would display the StockRecord formset, but I can create/update the Product without creating a StockRecord object. I'd like to show an error message when this occurs.

StockRecord form/formset:

class StockRecordForm(forms.ModelForm):

    def __init__(self, product_class, *args, **kwargs):
        super(StockRecordForm, self).__init__(*args, **kwargs)


        # If not tracking stock, we hide the fields
        if not product_class.track_stock:
            del self.fields['num_in_stock']
            del self.fields['low_stock_threshold']
        else:
            self.fields['price_excl_tax'].required = True
            self.fields['num_in_stock'].required = True

    class Meta:
        model = StockRecord
        exclude = ('product', 'partner', 'num_allocated')


BaseStockRecordFormSet = inlineformset_factory(
    Product, StockRecord, form=StockRecordForm, extra=1)


class StockRecordFormSet(BaseStockRecordFormSet):

    def __init__(self, product_class, *args, **kwargs):
        self.product_class = product_class
        super(StockRecordFormSet, self).__init__(*args, **kwargs)

    def _construct_form(self, i, **kwargs):
        kwargs['product_class'] = self.product_class
        return super(StockRecordFormSet, self)._construct_form(
            i, **kwargs)

StockRecord model(excerpt):

class AbstractStockRecord(models.Model):

    product = models.ForeignKey(
        'catalogue.Product', related_name="stockrecords",
        verbose_name=_("Product"))
    partner = models.ForeignKey(
        'partner.Partner', verbose_name=_("Partner"),
        related_name='stockrecords')

    partner_sku = models.CharField(_("Partner SKU"), max_length=128)
    price_currency = models.CharField(
        _("Currency"), max_length=12, default=settings.OSCAR_DEFAULT_CURRENCY)

    price_excl_tax = models.DecimalField(
        _("Price (excl. tax)"), decimal_places=2, max_digits=12,
        blank=True, null=True)

    price_retail = models.DecimalField(
        _("Price (retail)"), decimal_places=2, max_digits=12,
        blank=True, null=True)
¿Fue útil?

Solución

What you want to do is ensure there is at least 1 valid formset submitted when saving the main form?

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