6

Looking for some guidance because I'm entering new territory here in terms of my Django experience. I'm writing a reprographics request app so have created a couple of models:

class Job(models.Model):
  id = models.AutoField(primary_key=True) # AutoField?


class Resource(models.Model):
  id = models.AutoField(primary_key=True) # AutoField?
  job = models.ForeignKey(Job)
  file = models.FileField(upload_to="repro/")

The admin view includes the resource as inline

class ResourceInline(admin.TabularInline):
        model = Resource
        extra = 0

class JobAdmin(admin.ModelAdmin):
    model = Job
    list_display = ['requestedby','account','requestdate','requireddate','noofsides','noofcopies']
    list_filter = ['requireddate']
    search_fields = ['requestedby','account']
    form = JobForm
    fieldsets = [
        (None, {'fields': ['requestedby','account','requestdate','requireddate','noofsides','noofcopies'] }),
        ('Requirements', {'fields': ['color','sided','paper','finishing']}),
        ('Additional Information', {'fields': ['additionalinfo']}),

    ]
    inlines = [ResourceInline]
admin.site.register(Job,JobAdmin)

I'm planning to use dropzone.js and have got myself a change_form.html that I can customise but at this point I'm a bit lost. How can I replace the inline with my dropzone area and get it working?

Thanks for any help or pointers.

Chris

1

1 Answer 1

5

Use adminsortable2 to drag and drop in Django Admin. This is the github link.

So in you case, the code with adminsortable2 is this below.

"models.py":

from django.db import models

class Job(models.Model):
    id = models.AutoField(primary_key=True)
    my_order = models.PositiveIntegerField(default=0, blank=False, null=False)

    class Meta:
        ordering = ['my_order']
  
class Resource(models.Model):
    id = models.AutoField(primary_key=True)
    job = models.ForeignKey(Job)
    file = models.FileField(upload_to="repro/")
    my_order = models.PositiveIntegerField(default=0, blank=False, null=False)

    class Meta:
        ordering = ['my_order']

"admin.js":

from django.contrib import admin
from adminsortable2.admin import SortableAdminMixin, SortableInlineAdminMixin
from .models import Job, Resource

class ResourceInline(SortableInlineAdminMixin, admin.TabularInline):
    model = Resource
    extra = 0

@admin.register(Job)
class JobAdmin(SortableAdminMixin, admin.ModelAdmin):
    list_display = ['requestedby','account','requestdate','requireddate','noofsides','noofcopies']
    list_filter = ['requireddate']
    search_fields = ['requestedby','account']
    form = JobForm
    fieldsets = [
        (None, {'fields': ['requestedby','account','requestdate','requireddate','noofsides','noofcopies'] }),
        ('Requirements', {'fields': ['color','sided','paper','finishing']}),
        ('Additional Information', {'fields': ['additionalinfo']}),
    ]
    inlines = [ResourceInline]

Then, run this command below after writing the code above to save your sorting:

python manage.py reorder <app.model>

So, in your case, I don't know your app name so if your app name is "work" and I know your model names "Job" and "Resource" so run this command below to save your sorting:

python manage.py reorder work.Job work.Resource

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.