Django admin - 如何使用save_related()来解析内联表单中的数据?

时间:2016-04-28 22:44:56

标签: django django-models django-admin

当用户在django admin中保存相关(内联)数据以调用外部API时,我需要拦截。 我有以下型号:

class Hostel(models.Model):
    name = models.CharField(max_length=50)
    descr = models.CharField(max_length=200)
    ....

class User(models.Model):
    name = models.CharField(max_length=50, blank=False)
    nick = models.CharField(max_length=20, blank=False)
    password = models.CharField(max_length=20, blank=False)

class HostelGuest(models.Model):
    hostel = models.ForeignKey(Hostel)
    guest = models.ForeignKey(User)
    entry_date = models.DateTimeField(null=True,blank=True)

class HostelEntryPoint(models.Model):
    hostel = models.ForeignKey(Hostel)  
    getting_here = models.CharField(max_length=300, blank=False)
    distance = models.CharField(max_length = 30, blank = True)

在admin.py中,我在同一页面上作为父母和HostelGuest和EntryPoint作为孩子。每当HostelGuest中的某些数据发生变化时,我都需要查看它是否添加/删除/更新并调用外部API。我想过使用save_related()但是我无法解析收到的数据并找出所采取的行动。这是我所得到的,任何帮助将不胜感激!

class HostelEntryPointInline(admin.TabularInline):
    model = HostelEntryPoint
    extra = 3

class HostelGuestInline(admin.TabularInline):
    model=HostelGuest
    extra=4

class HostelAdmin(admin.ModelAdmin):
    inlines = [HostelGuestInline, HostelEntryPointInline]

    def save_related(self, request, form, formsets, change):
        super(HostelAdmin, self).save_related(request, form, formsets, change)
        logger = logging.getLogger(__name__)
        obj = form.instance
        obj.save()
        for formset in formsets:
            for f in formset:
                logger.error('form name:' + f.__class__.__name__)
                logger.error('form:' + f.cleaned_data['guest'])

admin.site.register(Hostel,HostelAdmin)

2 个答案:

答案 0 :(得分:1)

您可以在Form中使用admin.TabularInline

这是一个例子。

class HostelGuestForm(forms.ModelForm):
    # set your code.

class HostelGuestInline(admin.TabularInline):
    model=HostelGuest
    form = HostelGuestForm
    extra=4

如果你想调用外部API,这是一个解决方案。 您应该在class HostelAdmin

上输入内容
def save_formset(self, request, form, formset, change):
    if formset == formset_factory(HostelGuestForm):
        # this `if` is check for formset is for HostelGuest
        instances = formset.save(commit=False)
        # it returns your added, changed, deleted instance
        # if you don't have to check that instance is added or changed or deleted, below code is not required.
        for instance in instances:
            if instance.pk == None:
                # this is added
            elif:
                # check `change` for is changed or deleted
        formset.save_m2m()
    else:
        pass

def save_formset()调用save_related()不更改save_related,更改save_formset 的原因是针对其他inline form。因为内联formset不仅可以添加HostelGuest

答案 1 :(得分:1)

对于任何来这里寻求与Django M2M内联表单更改挂钩的人(像我一样),这就是我发现的简单而有效的方法:

(xT-x0, yT-y0, zT-z0) = (R + cylinderHeight) / R * (x-x0, y-y0, z-z0)

xT = (R + cylinderHeight) * (x-x0) / R + x0
yT = (R + cylinderHeight) * (y-y0) / R + y0
zT = (R + cylinderHeight) * (z-z0) / R + z0