Doing it right in Django - subclassing instead of hacking - django

I am using the threadedcomments module and need two changes:
- an additional field on the ThreadedComment model
- different fields on the form
I know the answer is to subclass but I'm not sure how to go about doing this - where does the code go?

As its not clear from you question. I am assuming you are talking about extending django.contrib.comments .
Yes you have to subclass it. Create your own application, and all code (extended models, forms, views etc.) goes there.
Important things, you wont add django.contrib.comments in INSTALLED_APPS list, but add your comment application name(which you inherit from django.contrib.comments).
Also you would need to add
COMMENTS_APP = 'my_comment_app'
to your settings.py
Here is very good example for doing exactly what you want to do.

Related

Why are my models not showing up in django admin site?

I have made alot of models and forgot to register them when I made them, after I realized I didn't register them I went and registered them the usual way (shown below). I've deleted the database and all migrations (including __pycache__) but haven't deleted the __pycache__ in the inner project folder (that holds settings.py) because I don't know if that would cause problems or not. I've tried using admin.register(Comment,admin) but that didn't work and, as you know, isn't necessary. I'm not sure what other information I would need to give so please let me know what else you need to know. Just so you know, I have 'django.contrib.admin' and 'django.contrib.sites' in the INSTALLED_APPS and also have path('admin/', admin.site.urls) in the project level urls.py
admin.register(PicturePost)
admin.register(VideoPost)
admin.register(TextPost)
admin.register(Comment)
admin.register(Report)
Please use below code because you are not using correct method to register the model. admin.register is method decorator for ModelAdmin class.
admin.site.register(PicturePost)
admin.site.register(VideoPost)
admin.site.register(TextPost)
admin.site.register(Comment)
admin.site.register(Report)
admin.register is a decorator that you should apply to custom ModelAdmin classes - not a function that you can use to register models.
You need to use admin.site.register instead:
admin.site.register(PicturePost)
etc.

Django how to make every view accept a kwarg?

I have a lot of apps running on my site and I was wanting to make all the views accept a certain kwarg without having to go in and edit them all manually? Is there a way to do this?
I suppose I should add it into the django base view class somewhere, but I am unsure exactly where to add it to in that?
Any ideas?
EDIT:
What I am trying to do is have translations set in my db under a certain model and then have the site default text areas be displayed in a certain language based on the url...
/eng/some/url
/esp/some/url
those two urls would display different languages, but I have to capture the variable and put it into each and every view which is quite cumbersome
Django already has some i18n support in urls, check it out. You need to activate django.middleware.locale.LocaleMiddleware by adding it to your settings.MIDDLEWARE_CLASSES and to tune your urlconf a bit by wrapping your urls with i18n_patterns.
The complete example is given in the docs, I see no sense copying it here.

Django admin: Change selected box of related fields to autocomplete

We have some models that are have a user as a foreign key. But with about 25000 users in our system, it's a bit daunting to find the one we need.
Is there a solution out there that's better than the select box? Maybe an autocomplete so we can start typing the user name / address? Or just a search box? When switching the related user for these objects, it's getting harder and harder with 25000 unsorted users.
Even just setting it to sort the users by username would be helpful.
I had this problem and my conclusion was to use an autocomplete field instead. It works pretty well in most cases. The only problem is when you have a lot of entries that are mostly the same. For example, in your case, if you type Robert for the name and there's a few hundred Robert entries in the list...
UPDATE
As mentions in shuckc's answer, Django 2.0+ admin as now autocomplete built in.
For older Django or to use outside of the admin (old answer)
There are many apps that add autocomplete to the Django admin:
django-autocomplete-light
django-extensions (ForeignKeyAutocompleteAdmin)
django-autocomplete (on google code)
django-ajax-selects
django-admin-autocomplete
django-autocomplete (tyrion)
My preferred one is the last one. It's well written, it can be used with the admin and outside of the admin, it works with ManyToManyFields, ForeignKeyFields, CharFields, etc.
I did a fork of this project for my client that adds some niceties like a lookup (loupe) button like the ForeignKeyRawIdWidget.
Django 2.0 admin has autocomplete built in, just set the autocomplete_fields field on the ModelAdmin class. e.g.
class QuestionAdmin(admin.ModelAdmin):
ordering = ['date_created']
search_fields = ['question_text']
class ChoiceAdmin(admin.ModelAdmin):
autocomplete_fields = ['question']
The simplest out-of-the-box solution is to add the field to your ModelAdmin's raw_id_fields -- then you'll get a pop-up window in which you can use the built-in searching/filtering and pagination control's to find and select the object you're after.
If you really want autocomplete, the other answers give a you reasonable starting point.
You can use the ForeignKeyRawIdWidget from django.contrib.admin.widgets. It renders FK relations as an input with a small button along-side which presents a searchable pop up.
There is an app for that (django-autocomplete).

Use custom Comment app and the default Comment from Django

I have a custom comment app but also want to use the original Comment app, without the added fields of my custom app. Is there a way to do this?
Thanks!
Sure, but if you named your custom app "comments" as well, you'll have to be extra careful to keep things straight. It would probably be best to import Django's comment system like so:
from django.contrib import comments as django_comments
You can then access the comment model like:
django_comments.Comment
Which will signal where it's actually coming from at a glance.

Reusing Admin forms for user views in django?

Django is making very nice forms after creating a models.py and an admin.py.
How can I reuse these forms (with the extra nice handling of foreign keys and many-to-many fields) in my own views?
ModelForm does only generate "simple" forms. Where do I get the extra batteries?
I was actually able to replicate those green buttons in my forms by following the instructions on this page: http://www.hoboes.com/Mimsy/hacks/replicating-djangos-admin/
A stock ModelForm will do almost all of what the admin does (ForeignKeys will turn into a dropdown select, ManyToManyFields will turn into a multiple-select).
The main exception would be the little green plus buttons for adding a new entry. It would be pretty hard to make those generic, as they depend on a number of admin-specific things: knowing where to find an add page for the linked model; JS to popup a window, close it on submit, and update the parent page; etc. You can dig into the admin and figure out how it implements those extra niceties, but there is not going to be a simple way to drop them into your code.
The other nicety you might be wanting is the filter_horizontal or filter_vertical alternative UIs for a ManyToManyField. Those are implemented as ordinary form widgets, so the potential is there for reusing them in your own code, but I'm guessing it'll take some experimentation and customization to make it work properly.