Django admin - hide remove link - django

How can I hide remove link in Django admin? I need to hide it in my code, not in permissions (only user with id=1 can see it no matter if any users are superusers)

This is the wrong way to do things so you're going to find that you're working against the grain to accomplish this. Permissions/Groups are the appropriate way to handle something like this.
However if you insist you're likely going to need to kludge this by overriding the default admin templates.

Related

django: is it possible to use the views that the admin panel uses?

Just learning django here, going through their website's tutorial.
When they take you through their admin panel, it's like, wow, all I have to do is define a model and django will give me a cool web page like this?
But that doesn't seem to be the case--it seems as if all that cool automatic functionality is only inside the admin panel. It seems that, if you want to have a cool add/change form, for instance, a form that looks and acts like the one in the admin panel, you're going to have to do all the layout and cool features by hand.
Am I wrong about this? Is there some way to make something that looks and works exactly like the admin's "change record" panel, but at your own url?
You are right, but I guess your expectations might be wrong.
Django's automatic admin site is one of the things that makes it very powerful. For example if you're building a large site, normally you'd also have to spend a lot of time to create a admin site, I mean A LOT! So Django provides a useful admin site automatically and it does a good job, which is great.
On the other hand your actual site is always going to be different so Django doesn't provide you the similar "automatic" ways as you expect. But it provides you the same building blocks, like ModelForms, ClassBasesView etc.. And of course, you have to put them together. But it's so much easier and faster to build sites with those blocks then doing it by yourself.
That being said, if you really like how admin site is looking and behaving then you can copy and use them on your main site, which will still require you to do some work.
(Couldn't comment, lacking reps)

django - Add another model without Popup

Is there any magic why to do this in admin panel of django?
Let me know
Off the top of my head, you could use JS to grab the popup link and load the HTML in a div on the page. But that means you need to override the admin template. This QA should be helpful.
It also means you need to capture the saving of the new house. So, when someone presses save, depending on the response, the div either closes or shows the errors WITHOUT refreshing th page. I'm not sure but I think you'll also need to override the admin view that does this to send back json responses.
OR
You could use JS to mute that link and call up your own url, which would allow you to create your own view with your own custom form that does this. This would give you a bit more control without having to hack away at the Admin functionality.
e.g /house/ajax_add
You'll need to refresh the options in the House dropdown though. So I don't think you can avoid extending the template, now that I think about it.
It seems like a lot of trouble, really. No magic way that I know of, but it's possible.
To avoid popups, you might want to look at https://github.com/FZambia/django-fm:
Django-fm allows to create AJAX forms for creating, editing, deleting
objects in Django. This is a very personalized approach to quickly
build admin-like interfaces.
A live example can be found on http://djangofm.herokuapp.com/.
I'm not sure how this integrates with the current admin site, but with some changes it should not be hard.

Django Admin: Need to conditionally display fields

What is the best way to conditionally display a field in the admin depending upon the values of other fields?
In particular I'm thinking about the add_form and change_form. Whenever a certain choice is selected I'd like to hide or disable some fields.
I'm thinking that this might require a javascript solution, but am wondering if there is a better (i.e. builtin) way to do this.
Bernhard is right. You might be able to hack the admin view and template to conditionally show/not show widgets for a field, but if you want to do it dynamically based on user behaviors in the admin, you'll be using javascript.
It's not so terrible, though. At least the Django admin templates have model- and instance-specific ids to give you granular control over your show/hide behavior.
My answer may sound esoteric - but I suspect the design direction you may be taking could benefit from some reconsideration. When an interface requires that much TLC to resort to AJAX to make for a superior UX I am inclined to not use Admin interface for that.
My personal view of the Admin interface of Django is that it is a great freebie that gives me an instant CRUD view at a basic level. I would refrain from piggy backing on it for those who need a more user friendly interface (even if some of these users are handling admin functions). You will acquire technical debt in trying to do these type of Ajax UI/UX and complex form validations using the Admin interface.
I think you should be able to create your own ModelForm and specify that the admin uses that (https://docs.djangoproject.com/en/1.7/ref/contrib/admin/#django.contrib.admin.ModelAdmin.form).
Use the _init__() method of the form to selectively display the fields.
I will give it a try and update this answer if it works.
(Actually re-reading the question, it depends. If "values of other fields" is set before the page is loaded, this idea should work. If you want an instant response - click one field and another appears/disappears, yes you will need JavaScript).

Best way to fix django admin preview of flatpage attached to multiple sites

I have flatpage attached to multiple sites. Its admin preview chooses
arbitrary site, which is quite obvious after debugging up to lines 35-36 of
django.contrib.contenttypes.views.shortcut().
What would be the best way of fixing this problem?
I see that the shortcut() function takes a request object, so I could just extract host from there, but I prefer to not patch the live server.
I haven't looked at catching admin url yet, so maybe someone can suggest some nice solution?
In my opinion, this could be considered a bug in Django, and at least a partial fix would be to check if the current SITE_ID is one of the sites related to the object, and if so use that one instead of an arbitrary one. You could file a ticket with a patch.
To fix it without patching Django, you might look into overriding the admin edit-form template for the flatpages model so that you can put the URL you want into that link instead of the default one that goes to the shortcut view. I haven't looked into it enough to know how clean that would be.
Another option might be to monkeypatch the Flatpage model with a get_absolute_url method that actually returns a complete absolute url, including the domain, based on Site.objects.get_current().domain.

Is there an easy way to provide a custom django admin action without allowing changes to models?

I have a custom action on a model Foo all set up and ready to go, complete with a new permission I've made.
Problem is, my administrators need the can_change_foo permission to view a change list and perform that custom action (which I don't want to award).
Is there an easier way to set this up without rewriting the model list admin view?
There is a horrid hack I can think of... Completely untested, obviously...
You could disable all other actions by overriding get_actions() and only allow for your custom action. Then you could follow T.Stone's suggestion here and completely disable links to edit individual instances of the model. What that would allow you to do is give your users the can_change_foo permission knowing that the only action they would be able to perform was yours.
Not pretty... Especially the part about not linking to the edit page...
Is re-writing the list admin view that bad? :-)
I ended up overriding the changelist_view() method on my ModelAdmin class, copying the defaults from django and just commenting out the permissions check. The list (at least the way I have it configured) doesn't have links to edit the individual objects, and even if it did, django raises a PermissionDenied if you try and edit an individual object. (since I never awarded the can_change permission).
It's a hack, and some monkeypatching, but until there's a separate permission for viewing a changelist, it works pretty well.