3

I'm trying to build an import feature/form in the django admin interface for a specific model.

I have already found the following question on Stackoverflow, however as I am new to django, I have not been able to wire it all up. Import csv data into database in Django Admin

I guess I understand how to work with Django objects and how to use the CSV reader module, but I have a heck of a time putting it all together in Django.

what i tried so far is this:

models.py

class RfidTag(models.Model):
"""
Available RFID-Tags from Importfile
"""

system = models.DecimalField(
    _('system'),
    max_digits=4,
    decimal_places=0,
)

tagId = models.DecimalField(
    _('tag ID'),
    max_digits=4,
    decimal_places=0,
)

serial = models.CharField(
    _('serial'),
    max_length=10,
)


# forms.py #
class RfidImport(forms.ModelForm):
file_to_import = forms.FileField()

class Meta:
    model = RfidTag
    fields = ("file_to_import",)

def save(self, commit=False, *args, **kwargs):
    form_input = RfidImport()
    file_csv = self.cleaned_data['file_to_import']
    csv.register_dialect('excel-new', delimiter=';', quoting=csv.QUOTE_NONE)
    records = csv.reader(file_csv, dialect='excel-new')
    for line in records:
        self.system = line[0]
        self.tagId = line[1]
        self.serial = line[2]
        form_input.save()
    datafile.close()

admin.py

class RfidTagAdmin(admin.ModelAdmin):
    list_display = ('system','tagId','serial')
    actions = ['import_tags']

    def get_urls(self):
        urls = super(RfidTagAdmin, self).get_urls()
        my_urls = patterns('',
            (r'^import/$', self.admin_site.admin_view(import_tags))
        )
        return my_urls + urls

    def import_tags(self, request, queryset):
        return HttpResponseRedirect("./import")

    import_tags.short_description = "Import new RFID tags"
    pass

admin.site.register(RfidTag, RfidTagAdmin)

views.py

@staff_member_required
def import_tags(request):
    if request.method == "POST":
        form = RfidImport(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            success = True
            context = {"form": form, "success": success}
            return HttpResponseRedirect("../")
    else:
        form = RfidImport()
        context = {"form": form}
        return HttpResponseRedirect("../")

My question is, is admin action actually the right way? Is there a better way to achieve what I am trying? And how do I wire this up? I have yet to see the form, after I select the import action and click "go".

Community
  • 1
  • 1
bjoern
  • 95
  • 2
  • 5
  • I think the admin is the right way. It provides you with built in authentication, and a straight forward integration. – dm03514 Sep 28 '11 at 15:31

1 Answers1

0

The admin is the right way, however i wouldn't be using an action for this, those are designed to function over a list of objects and you don't need that. For this case simply extend the admin/index.html template and add an href to your view. After that you create a normal form in which you do your processing

armonge
  • 3,108
  • 20
  • 36
  • Ok, i managed to extend the admin change_list of the app with a link to the import view. The view is called, but it throws a TemplateSyntaxError. Maybe trying to use the change_form template of the admin instead of writing my own was a bad idea, I don't know. I'm looking for an easy way to keep the look and feel of the default admin. – bjoern Oct 25 '11 at 09:32
  • My question has been solved here: http://stackoverflow.com/questions/7889139/django-admin-integrating-custom-forms – bjoern Oct 27 '11 at 06:15