I am using django admin tabular-inline admin interface.
model.py
class Issues(TimeStampedModel):
issue_owner = models.ForeignKey(USER_MODEL, related_name='issue_owner')
issue_no = models.CharField(max_length = 500, null = True, blank = True)
class IssueComments(TimeStampedModel):
comment_owner = models.ForeignKey(USER_MODEL, related_name='comment_owner')
issue = models.ForeignKey(Issues, null=True, blank=True)
comment = models.TextField(null=True, blank=True)
I am trying to use tabular-inline in admin
admin.py
class IssueCommentsAdmin(admin.TabularInline):
model = IssueComments
extra = 1
def formfield_for_foreignkey(self, db_field, request=None,**kwargs):
field = super(IssueCommentsAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
if db_field.name == 'comment_owner':
if request.user is not None:
field.queryset = field.queryset.filter(username = request.user.username)
if not field.queryset:
field.queryset = field.queryset.all()
else:
field.queryset = field.queryset.none()
return field
class IssuesAdmin(admin.ModelAdmin):
model = Issues
list_display = ('issue_no', 'title', 'owner_phone_number', 'status', 'issue_priority', 'classification')
inlines = [ IssueCommentsAdmin ]
def render_change_form(self, request, context, *args, **kwargs):
context['adminform'].form.fields['assigned_to_user'].queryset = User.objects.filter(is_staff=True)
return super(IssuesAdmin, self).render_change_form(request, context, args, kwargs)
i want to restrict the comment_owner to logged in user only in choice-field. I am able to do that also but issue i am facing here is for the comments whiche already have the comment_owner i want to keep that as it is. Here comments which have comment_owner are not getting pre selected.
A little late but, for anyone
You should create another Model Admin class in admin.py and give permissions for this object. I recommend you change your classnames.
class IssueCommentsInline(admin.TabularInline):
...
class IssueCommentsAdmin(admin.ModelAdmin):
...
admin.site.register(IssueComments,IssueCommentsAdmin)
In this way, you can edit all data with get_queryset and field data with get_field_queryset.
regards!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.