Login

Drag sort inline copy

Author:
hieunv495
Posted:
January 17, 2018
Language:
Python
Version:
1.8
Score:
0 (after 0 ratings)

Inline

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* models.py */
from django.db import models
from django.utils.text import slugify
# Create your models here.


def get_unique_slug(model,field_name,value):
    max_length = model._meta.get_field(field_name).max_length
    slug = slugify(value)
    num = 1
    unique_slug = '{}-{}'.format(slug[:max_length - len(str(num)) - 1], num)
    while model.objects.filter(** {field_name: unique_slug}).exists():
        unique_slug = '{}-{}'.format(slug[:max_length - len(str(num)) - 1], num)
        num += 1
    return unique_slug

class Post(models.Model):
    name = models.CharField(max_length=255)
    description = models.TextField()

    slug = models.SlugField(max_length=5, unique=True, blank=True, editable=False)

    slug2 = models.SlugField(max_length=10, blank=True, editable=False)

    def __str__(self):
        return self.slug

    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = get_unique_slug(self.__class__,'slug',self.name)
        if not self.slug2:
            self.slug2 = get_unique_slug(self.__class__,'slug2',self.name)
        super().save(*args, **kwargs)


class Image(models.Model):
    post = models.ForeignKey(Post, related_name='images')
    file = models.ImageField(upload_to='upload')
    position = models.PositiveSmallIntegerField(default=0)

    class Meta:
        ordering = ['position']

    def __str__(self):
        return '%s - %s ' % (self.post, self.file)


/* admin.py */
from django.contrib import admin
from django import forms
from .widgets import AdminImageWidget
from .inputs import AdvancedImageInput
from .models import *
# Register your models here.

admin.site.register(Image)

class ImageForm(forms.ModelForm):
    # file = AdvancedImageInput()
    class Meta:
        model = Image
        widgets = {
            'file': AdminImageWidget
        }

        fields = '__all__'

class ImageInline(admin.StackedInline):
    form = ImageForm
    model = Image
    extra = 0

class PostForm(forms.ModelForm):
    model = Post
    class Media:
        js = (
            '/static/js/jquery.min.js',
            '/static/js/jquery-ui.min.js',
            '/static/js/inline-sort.js',
        )

class PostAdmin(admin.ModelAdmin):
    form = PostForm
    inlines = [ImageInline]

    def save_model(self, request, obj, form, change):
        super(PostAdmin,self).save_model(request, obj, form, change)
        # obj.save()

        for afile in request.FILES.getlist('photos_multiple'):
            obj.images.create(file=afile)

admin.site.register(Post, PostAdmin)

"""
/* menu-sort.js */
jQuery(function($) {
    $('div.inline-group').sortable({
        /*containment: 'parent',
        zindex: 10, */
        items: 'div.inline-related',
        handle: 'h3:first',
        update: function() {
            $(this).find('div.inline-related').each(function(i) {
                if ($(this).find('input[id$=position]').val()) {
                    $(this).find('input[id$=position]').val(i+1);
                }
            });
        }
    });

    $('div.inline-group').find('div.inline-related').each(function(i) {
        if ($(this).find('input[id$=position]').val()) {
            $(this).find('input[id$=position]').val(i+1);
        }
    });

    $('div.inline-related h3').css('cursor', 'move');
    // $('div.inline-related').find('input[id$=position]').parent('div').hide();
});

"""

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.