theatlantic / django-nested-admin

Django admin classes that allow for nested inlines
http://django-nested-admin.readthedocs.org/
Other
727 stars 101 forks source link

bug when choose Foreignkey field in an inline #234

Closed junwenwaynepeng closed 1 year ago

junwenwaynepeng commented 1 year ago

My models.py:

class Project(Timestemp):
    datart_id = models.CharField(default='year001', unique=True, max_length=10, help_text='AR-year001', verbose_name='案號')
    name = models.CharField(max_length=200, verbose_name='案名')
    # 業主
    content_type = models.ForeignKey(ContentType, null=True, blank=True, related_name='content_type_project', on_delete=models.CASCADE)
    limit_content_type = models.Q(app_label='v1', model='client') | models.Q(app_label='v1', model='goverment')
    object_id = models.IntegerField(null=True, blank=True)
    content_object = GenericForeignKey('content_type', 'object_id')
    content_object.short_description = '甲方'
    # 專案主管
    creater = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='creater', on_delete=models.SET_NULL, null=True, blank=True, verbose_name='專案啟動人')
    manager = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='manager', on_delete=models.SET_NULL, null=True, blank=True, verbose_name='PM')

class Folder(Timestemp):
    project = models.ForeignKey('Project', on_delete=models.CASCADE, verbose_name='專案')
    name = models.ForeignKey('FolderLabel', null=True, blank=True, on_delete=models.PROTECT, verbose_name='資料夾')
    file_name = models.CharField(max_length=50, null=True, blank=True, verbose_name='檔案名')
    creater = models.ForeignKey(settings.AUTH_USER_MODEL, editable=False, related_name='folder_creater', on_delete=models.CASCADE, verbose_name='檔案創始人')
    last_modifier = models.ForeignKey(settings.AUTH_USER_MODEL, editable=False, related_name='folder_last_modifier', on_delete=models.CASCADE, verbose_name='最後更動檔案人')
    file = models.FileField(upload_to=get_file_upload_path, verbose_name='上傳檔案')

class FolderLabel(models.Model):
    name = models.CharField(max_length=100, verbose_name='資料夾名稱')
    note = models.TextField(null=True, blank=True, verbose_name='備註')

and my admin.py

@admin.register(Project)
class ProjectAdmin(nested_admin.NestedModelAdmin): #class ProjectAdmin(admin.ModelAdmin):
    inlines = [RepositoryInline, ContractInline, ProcessInline, InternalClaimInline, FolderInline]
    #inlines = [ProcessInline, FolderInline]
    fieldsets = [
        (None, {
            'fields' : (
                'datart_id', 
                'name'
                )
            }),
        ('業主需求',{
            'fields' : (
                'party_a', 
                )
            }),
        ('專案主管', {
            'fields' : (
                ('creater', 'manager'),
                )
            })
        ]
    list_display = ['datart_id', 'name', 'party_a', 'date_created', 'date_modified', 'manager', 'stage']
    list_display_links = ['datart_id', 'name']
    readonly_fields = ('party_a', 'stage')
    search_fields = ['datart_id', 'name']
    list_filter = ['date_created']
    def party_a(self, obj):
        return obj.content_object
    def stage(self, obj):
        return obj.process_set.filter(is_finished=False).first().process_label
    party_a.short_description = '甲方'
    stage.short_description = '進度'
    def save_formset(self, request, form, formset, change):
        instances = formset.save(commit=False)
        for obj in formset.deleted_objects:
            obj.delete()
        for instance in instances:
            instance.creater = request.user
            instance.last_modifier = request.user
            instance.save()
        formset.save_m2m()

class FolderInline(nested_admin.NestedTabularInline): # class FolderInline(admin.TabularInline):
    model = Folder
    sortable_field_name = 'name'
    readonly_fields = ['creater', 'last_modifier']
    extra = 0

I have tried the django's original TabularInline, and everything works fine. However, when I change the django's TabularInline to nested_admin's Tabular inline, the following error arises:

ValueError: Cannot assign "0": "Folder.name" must be a "FolderLabel" instance.

The bug is even when I choose an object in a foreignkey field of an inline form and send Post request, the information isn't carried along.

I could provide more detail information if you need. However, I believe this is a bug in nested_admin. Hope this won't be too hard to fix.

junwenwaynepeng commented 1 year ago

https://stackoverflow.com/questions/73709820/data-of-foreign-keys-are-not-saving-in-django-admin-nested-inlines

I find a solution here. However, I stil conisider this is a bug that needs to be fixed because I want to sort the inline by the name.

fdintino commented 1 year ago

sortable_field_name is exclusively for keeping track of the order while drag-and-drop sorting. If you want the results to be sorted by name then you can accomplish that by omitting the sortable_field_name and adding the ordering to the model's class Meta. Though that will only sort entries on load that have already been saved, there wouldn't actually be a way of dynamically sorting by the name while the names are being added and edited.