Hello everyone.
I have a small problem that I haven’t been able to solve on my own yet.
Here is my code:.
models.py:
class Article(models.Model):
ID = models.AutoField(primary_key=True)
name = models.CharField(unique=True, max_length=100)
price = MoneyField(blank=True, null=True, decimal_places=2, max_digits=8, default_currency='EUR')
class Intervention(models.Model):
ID = models.AutoField(primary_key=True)
start_date = models.DateField()
description = models.TextField()
ID_article = models.ManyToManyField(Article, through="DetailsArticle", blank=True)
class DetailsArticle(models.Model):
ID = models.AutoField(primary_key=True)
ID_article = models.ForeignKey(Article, on_delete = models.CASCADE)
ID_intervention = models.ForeignKey(Intervention, on_delete = models.CASCADE)
quantity = models.IntegerField(null=True, blank=True, default='1')
resources.py:
class ArticleResource(resources.ModelResource):
ID = fields.Field(attribute = 'ID', column_name = 'ID Article')
name = fields.Field(attribute = 'name', column_name = 'Article')
price = fields.Field(attribute = 'price', column_name = 'Price')
class Meta:
model = Article
import_id_fields = ('ID',)
class DettaglioArticoliResource(resources.ModelResource):
ID = fields.Field(attribute = 'ID', column_name = 'ID Details')
ID_intervention = fields.Field(attribute = 'ID_intervention', column_name = 'Intervention')
ID_article = fields.Field(attribute = 'ID_article', column_name = 'Articles',
widget=widgets.ForeignKeyWidget(Article, 'name'))
class Meta:
model = DetailsArticle
class InterventiResource(resources.ModelResource):
ID = fields.Field(attribute = 'ID', column_name = 'ID Intervention')
start_date = fields.Field(attribute = 'start_date', column_name = 'Start Date')
description = fields.Field(attribute = 'description', column_name = 'Description')
ID_article = fields.Field(attribute = 'ID_article', column_name = 'Articles',
widget=widgets.ManyToManyWidget(model=Article, separator=',', field='name'))
quantity = fields.Field(attribute='detailsarticle__quantity', column_name='Quantity')
class Meta:
model = Intervention
import_id_fields = ('ID',)
def dehydrate_quantity(self, interv):
details_article = interv.dettaglioarticoli_set.all().order_by('ID_article')
quantity_list = [str(da.quantity) for da in details_article]
return ', '.join(quantity_list)
The export of the “Intervention” table (with django_import_export) works correctly, because in a single exported file I have both the name of the article and the respective quantity used for each Intervention.
The problem instead occurs during the import of the “Intervention”.
Because the “Intervention” table is imported correctly, but I can’t get the “through” DetailsArticle table to automatically update as well.
I would like that when I import an “Intervention” table, if there are rows from the DetailsArticle table, they are updated.
However, if there are new rows, new corresponding rows are automatically created also in the DetailsArticle table.
How can I do?
Sorry, but I’m really a beginner in this field.
I’m sorry I haven’t done it on my own yet.
I accept all your possible advice and help.
I thank you from the bottom of my heart for your constant and indispensable help.
Sorry for my bad English.
Thanks everyone again.
Happy Coding!