简体   繁体   中英

get value of field from other form django admin

simply i want to build a small email application between users(admins) in django admin i was successful to make users send and receive messages, in the next step: users should be able to reply to received messages. simply i added a "Reply" button in indox form to redirect it to outbox form, the problem now is how can i add the to:mail in the request?

my model.py

class Mail(models.Model):
from_user = models.ForeignKey(settings.AUTH_USER_MODEL,
                              verbose_name=_('From User'),
                              related_name='sent_mails')
to_user = models.ForeignKey(settings.AUTH_USER_MODEL,
                            verbose_name=_('To User'),
                            related_name='recieved_mails')
subject = models.CharField(_('Subject'), max_length=255)
body = models.TextField(_('Body'))
read = models.BooleanField(default=False)

created_at = models.DateTimeField(auto_now_add=True)
last_update = models.DateTimeField(auto_now=True)

admin.py

class Inbox(Mail):

     class Meta:
         app_label = 'internal_mails'
         proxy = True
         verbose_name = _("Inbox")
         verbose_name_plural = _("Inbox")


class Outbox(Mail):

    class Meta:
        app_label = 'internal_mails'
        proxy = True
        verbose_name = _("Outbox")
        verbose_name_plural = _("Outbox")

class InboxAdmin(MailAdmin):
    list_display = ('id', 'from_user', 'subject',)
    exclude = ('read',)

    def get_queryset(self, request):
       queryset = super(InboxAdmin, self).get_queryset(request)
       return queryset.filter(to_user=request.user)

    def has_add_permission(self, request, obj=None):
        return False

    def changeform_view(self, request, object_id=None, form_url='', extra_context=None):
        response = super(InboxAdmin, self).changeform_view(
            request, object_id=object_id,
            form_url=form_url, extra_context=extra_context
        )
        if isinstance(response, HttpResponseRedirect):
            obj = Mail.objects.get(id=object_id)
            print(obj.from_user)
            response = HttpResponseRedirect('/'.join(response.url.split('/')[:-2] + ['outbox/add/']))
        else:
           obj = Inbox.objects.get(id=object_id)
           obj.read = True
           obj.save()
        return response


class OutboxAdmin(MailAdmin):
    list_display = ('id', 'to_user', 'subject',)
    list_filter = ('to_user',)
    exclude = ('from_user', 'read',)

    def get_queryset(self, request):
        queryset = super(OutboxAdmin, self).get_queryset(request)
        return queryset.filter(from_user=request.user)

    def save_model(self, request, obj, form, change):
        obj.from_user = request.user
        obj.save()


admin.site.register(Inbox, InboxAdmin)
admin.site.register(Outbox, OutboxAdmin)

here is how i solved the problem i sent the id of the user in url Ex: 127.0.0.1:8000/en/outbox/add/?to_user=1

when clicking in reply button it redirect to outbox with user id

in InboxAdmin class

    if isinstance(response, HttpResponseRedirect):
        obj = Mail.objects.get(id=object_id)
        url = reverse("admin:internal_mails_outbox_add")
        return HttpResponseRedirect(url + "?to_user=" + str(obj.from_user.id))

additional note if you need to know how to reversing the URL in Django-admin you may need to read this https://docs.djangoproject.com/en/dev/ref/contrib/admin/#reversing-admin-urls

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM