ModelAdmin missing a couple of key features, or am I doing this wrong?

47 lượt xem
Chuyển tới thư đầu tiên chưa đọc

th...@copperleaf.com

chưa đọc,
18:53:15 15 thg 9, 201615/9/16
đến Django users
I have a ModelAdmin subclass (code at: <https://dpaste.de/0LAt>) and I need to do a couple of things, but the functionality seems to be missing:

1. I need to access the model instance to perform some initialization, but there's no instance member set. There are a few questions on Stack Overflow on this topic, so I know I'm not alone.

2. I need to initialize my form with a parameter derived from the model instance. If this were a View, I could just override get_form_kwargs and be done with it, but ModelAdmin doesn't have it.

Is there a reason why those two features are missing from ModelAdmin? Maybe they could be added (and I could open a PR)? Or is there a better way to do what I need to do?

Tim Graham

chưa đọc,
20:34:50 15 thg 9, 201615/9/16
đến Django users
I think these ideas have been floated before. If you look through the Trac tickets you might find something related. However, your subclass where you store "self.instance" on ModelAdmin is a no no due to thread safety. See http://stackoverflow.com/questions/3388111/modeladmin-thread-safety-caching-issues for an explanation.

th...@copperleaf.com

chưa đọc,
21:00:02 15 thg 9, 201615/9/16
đến Django users
Hi Tim, thanks for the reply :)

It's not that I want to store the model instance on self.instance, it's just the solution I was able to come up with, given the problem "I need to get to the model instance for this HTTP request". If there's a better way to do it, I'm all ears!

T

th...@copperleaf.com

chưa đọc,
21:22:36 15 thg 9, 201615/9/16
đến Django users
I'm having trouble finding any related tickets other than this one, which is 8 years old:



On Thursday, September 15, 2016 at 5:34:50 PM UTC-7, Tim Graham wrote:

th...@copperleaf.com

chưa đọc,
21:27:40 15 thg 9, 201615/9/16
đến Django users
I just realized that get_form is already passed the model instance as obj, so that fixes getting he model instance (for this method at least -- hopefully I don't need to override another method that doesn't get obj). Still need a better way to customize how a form instance is initialized, though...

Andrea D'Amore

chưa đọc,
04:46:32 16 thg 9, 201616/9/16
đến django...@googlegroups.com
On 16 September 2016 at 00:53, <th...@copperleaf.com> wrote:
> I have a ModelAdmin subclass (code at: <https://dpaste.de/0LAt>)

Gives a 404:

The requested URL /0LAt was not found on this server.


I'm missing the rationale of using an external paste service while
you're already using a text-based medium, the mailing list, that's
able to accomodate what's (hopefully) a small code excerpt, in worst
case scenario I think it'll accept an attachment as well.

This also has the added bonus of getting archived and never result in
broken links and missing content, like it's likely to happen at some
point with external services.


--
Andrea

th...@copperleaf.com

chưa đọc,
09:50:44 16 thg 9, 201616/9/16
đến Django users
Apologies, I updated it.


class MyModelAdmin(ModelAdmin):
    model = MyModel
    form = MyForm
 
    def get_form(self, request, obj=None, **kwargs):
        form_class = super(MyModelAdmin, self).get_form(request, obj=obj, **kwargs)
 
        some_parameter_for_one_thing = self.get_some_parameter_for_one_thing()
 
        from django.forms.models import ModelFormMetaclass
        class FormMetaclass(ModelFormMetaclass):
            def __new__(mcs, name, bases, attrs):
                attrs["some_parameter_for_one_thing"] = some_parameter_for_one_thing
                return super(FormMetaclass, mcs).__new__(mcs, name, bases, attrs)
 
        another_parameter_for_another_thing= obj.another_parameter_for_another_thing
 
        import six
        class FormWrapper(six.with_metaclass(FormMetaclass, form_class)):
            def __init__(self, *args, **kwargs):
                data = {"another_parameter_for_another_thing": six.text_type(another_parameter_for_another_thing)}
                super(FormWrapper, self).__init__(data, *args, **kwargs)
 
        return FormWrapper

James Schneider

chưa đọc,
04:03:26 17 thg 9, 201617/9/16
đến django...@googlegroups.com

(OT Disclaimer...not directed at this OP.)

I would [mostly] disagree.

Rationale: A majority of my responses (including this one) are written on my phone while I'm bored and out and about, or at night during my pre-bed email check. While the GMail app is great, I've never seen it employ any sort of code highlighting or sane code wrapping or indentation, beyond copying directly out of an SO post or other snippet service. Probably because the GMail app is an email client, not an IDE. There is also no real way to preview the effects of spacing and line wrapping on multiple devices, screen sizes, HTML vs. plain text, Unicode handling, etc. Basically, there's a bunch of ways a direct paste from an IDE can go sideways in an email, with an increasing likelihood for large code chunks. Difficult-to-read code directly in an email will deter responses. It does for me, anyway.

For small code snippets, absolutely, paste them directly in and just make sure your indentation is at least consistent. If you have more than a dozen or so SLOC, though, try and reduce the provided code to the stuff that's necessary (which will often force an OP to really evaluate what their code is doing and aid in troubleshooting), or make use of an external service such as dpaste.de.

Mobile devices cannot always open external files, especially those with less common extensions such as .py. It also fills up your mailbox, needlessly. I rarely, if ever, open the attachments sent out to this list.  Trying to keep track of the original question compared with the code in a separate application screen on a mobile device is nearly impossible.

A majority of the code in this list doesn't need to live forever once the OP has a proper answer. A response with a summation of the correct solution is almost always the key piece needed when searching through the archives or Googling, at least in my experience with this list and other forums.

With that being said, sometimes the OP may not know the exact pieces that are needed for inspection, or there are multiple files involved, so an attachment may be necessary. Code snippet services also lack in this area.

IMHO, you hijacked this thread to insert your preference for posts, without offering a single bit of advice to the OP, which reflects poorly on you. I would offer advice to the OP, but I have none because I have little experience using the built-in admin.

I would recommend that you offer suggestions for posting guidelines to the moderators of this list and/or the Django devs. Help move this community forward rather than criticising those requesting assistance, especially on a point of personal preference.

Just my personal opinion; if I'm out of line, someone please let me know.

-James

Trả lời tất cả
Trả lời tác giả
Chuyển tiếp
0 tin nhắn mới