python - An efficient way to save parsed XML content to Django Model -


this first question best conform question guidelines. i'm learning how code please eli5.

i'm working on django project parses xml django models. podcast xmls.

i have code in model:

    django.db import models     import feedparser     class channel(models.model):         channel_title = models.charfield(max_length=100)          def __str__(self):             return self.channel_title      class item(models.model):         channel = models.foreignkey(channel, on_delete=models.cascade)         item_title = models.charfield(max_length=100)          def __str__(self):             return self.item_title      radiolab = feedparser.parse('radiolab.xml')     if channel.objects.filter(channel_title = 'radiolab').exists():         pass     else:         channel_title= radiolab.feed.title         = channel.objects.create(channel_title=channel_title)         a.save()       episode in radiolab.entries:         item_title = episode.title         channel_title = channel.objects.get(channel_title="radiolab")         b = item.objects.create(channel=channel_title, item_title=item_title)         b.save() 

radiolab.xml feed i've saved locally radiolab podcast feed.

because code run whenever python manage.py runserver, parsed xml content sent database want happens every time runserver, meaning duplicate records.

i'd love in finding way make happen once , dry mechanism adding different feeds they're parsed , saved database preferably feed url submitted via forms.

if don't want run every time, don't put in models.py. thing belongs there model definitions themselves.

stuff happens in response user action on site goes in view. or, if want done admin site, should go in admin.py file.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -