barii
barii

Reputation: 375

django-import-export package get a Foreignkey value while Export to XL file

First pardon me for my bad english. I am very new in Django Framework. I want to Export a foreignkey value called title which is in 'Product' model. There is multiple Foreignkey Relations between different django app models. Please take a look below all of this models:

products/models.py

class Product(models.Model):
    title = models.CharField(max_length=500)
    brand = models.ForeignKey(
        Brand, on_delete=models.CASCADE, null=True, blank=True)
    image = models.ImageField(upload_to='products/', null=True, blank=True)
    price = models.DecimalField(decimal_places=2, max_digits=20, default=450)
    old_price = models.DecimalField(
        default=1000, max_digits=20, decimal_places=2)
    weight = models.CharField(
        max_length=20, help_text='20ml/20gm', null=True, blank=True)
    size = models.CharField(
        max_length=10, help_text='S/M/L/XL/32/34/36', null=True, blank=True)
    active = models.BooleanField(default=True)
    timestamp = models.DateTimeField(auto_now_add=True)
    update = models.DateTimeField(auto_now=True)

carts/models.py

class Entry(models.Model):
    product = models.ForeignKey(Product, null=True, on_delete=models.CASCADE)
    cart = models.ForeignKey(Cart, null=True, on_delete=models.CASCADE)
    quantity = models.PositiveIntegerField(default=1)
    price = models.DecimalField(default=0.00, max_digits=100, decimal_places=2)
    last_purchase_price = models.DecimalField(
        default=0.0, max_digits=20, decimal_places=15)
    weight_avg_cost = models.DecimalField(
        default=0.0, max_digits=20, decimal_places=15)
    updated = models.DateTimeField(auto_now=True)
    timestamp = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['-timestamp']

And my working models is analytics/models.py, from where I want to export the product title in XL sheet.

class AllReportModel(models.Model):
    # All report model
    invoice = models.ForeignKey(Invoice, on_delete=models.CASCADE, null=True, blank=True)
    ws_invoice = models.ForeignKey(WholesaleInvoice, on_delete=models.CASCADE, null=True, blank=True)
    entry = models.ForeignKey(Entry, on_delete=models.CASCADE, null=True, blank=True)
    stock_quantity = models.IntegerField()
    timestamp = models.DateTimeField(auto_now_add=True)
    update = models.DateTimeField(auto_now=True)

and Finally analytics/admin.py is :

class AllReportModelResource(resources.ModelResource):

    class Meta:
        model = AllReportModel
        fields = ('id', 'invoice', 'ws_invoice', 'entry', 'stock_quantity')


    class AllReportModelAdmin(ImportExportModelAdmin):
        resource_class = AllReportModelResource
        list_display = ['invoice', 'ws_invoice', 'entry', 'timestamp']
        search_fields = ['invoice']
    
    
    admin.site.register(AllReportModel, AllReportModelAdmin)
 

Help will be highly appreciated.

Upvotes: 0

Views: 67

Answers (1)

Radwan Abu-Odeh
Radwan Abu-Odeh

Reputation: 2045

I've been into this situation before, its solution is very easy.

You can refer to django-import-export documentation for advanced data manipulation on export Click HERE.

Upvotes: 1

Related Questions