Notification to user from admin - django

I am new in Django and don`t know the best way to realise next functionality in my project.
In my django project I have users with different roles. One of them is admin who can create project. I mean that I have model with fields (project_name, project_managers) which filled by admin. The managers of the project are users of the system, admin can choice some of them. After all how to show them notification in real time that they was managers of the current project. Is it makes by websockets or Jquery or something else. I need some ideas how to realise that with detail explanation if it possible.

You can use post_save signal to implement your notification functionality. This signal will be called once the model object is saved.

Related

Django: Last modified by and created by user automatic saving

The age-old question: How can I automatically save last_modifed_user in django models?
I found in several places this general process how to do it using thread local. I'm hesitant to simply implement it that way because I'm not entirely sure of the consequences it has and because all these posts are old.
Is using thread local still the "recommended" way of doing this in django 3? Or does django3 have a better options of doing it?
No, this hasn't changed. Simply because separation of concern is an architectural principle of MVC (model-view-controller), which is also how Django (model-view-template) and most web frameworks with ORM are architected. Models know nothing about the request, it's not available (and in many cases there isn't a request at all when a model is saved, think of management commands or regular tasks running in the background).
The alternative to thread local is to make sure you implement it yourself in the controller layer (view layer in Django):
Create a view mixin that you can mix with all the generic views that use the ModelFormMixin to save the user into the model (ModelFormMixin.form_valid()). Or combine it with a form mixin where the user is passed to the form (FormMixin.get_form_kwargs()) and saved when the form is saved (ModelForm.save()).
Create a ModelAdmin mixin that does the same when saving a model in the django admin site.
This of course means someone on your team may forget to do it when creating new views and forms. The link you posted contains an answer as to the advantages and disadvantages of using thread local.

Add a function in models.py or managers.py

I'd like to send an activation link to a new registered user. Should I write my function in my models.py or managers.py?
It's always confused to me to know where put the function, even after reading the documentation.
None of them, models and managers are related to application data. Sending emails are related to the logic of your app: actions, decisions, answers ... so you should do this in a view.
If you need to save time, you can use Django Registration as #karthikr suggests to you, this app is a good wrapper for reaching this aim.
I've assumed you need to send the link in the moment that the user has registered, but if you want to do this in other moment you can use a scheduled task: a django cron, an external python process or ...; that is up to you: your porpuse, the design of your app.
This blog could help you understand the use of managers better.
I would put the activation link in managers, because it would be easier to manage the various activities around it - resend activation link, validation, etc. It could be done with models too, but managers make it more modular.
Django Registration is quitely widely used for registration - you could see how it is implemented there as well.
Yes , you can write your customized function in models.py but not sure about manage.py .
I have used signal method in models.py to for mailservice facility.

django: Hide Model Types with no instances on Admin page

Say I have an app with ModelA, ModelB, and ModelC
For my app's admin page (/admin/app), how do I hook into the display of the "App administration" page so I may hide Model types that are empty (have no instances)?
That's a big ask, because you'd have to dynamically register/unregister apps according to the results of a database/ORM query (a count(), at least) for each of the models that each of the INSTALLED_APPS contains.
Every single time you viewed the admin.
While that in itself is unpleasant enough, bear in mind that admin.py for each app is processed at server restart/reload time (IIRC), so you could't hope to hook up something cute like those DB lookups there, as it'd only get run once and not reflect the actual state of models currently have objects stored in your database.
Better move: leave it be. If there are no objects to view for a given model, then there are no objects to view for a given model.
If you're concerned about your client/user making new models in there when they shouldn't, then that's a combination of user education and admin user permissions that you can sort out.
As said in a few places: "The Admin is not your app." If the customization goes beyond the trivial, it's time to write your own views. You can still hook them in to the admin site by overriding the base admin template and even serving them from the same root path as the rest of your admin.

Tracking changes to Django Model instances

When you create or modify an object instance in Django's admin, a changelog entry is created. This is really nice for fairly obvious reasons.
However my model's instances created by a normal user outside of the admin interface. No changelog is recorded to note its creation (not a huge issue) but I would like to track edits the user makes.
I also want to show the user this full log (user+admin edits) in the frontend so I need a way to pull the changelog out.
My question: how? Is there a one-line switch I can flick to enable full logging or do I have to dig in and do something on my user's edit form logic?
django-reversion is an app designed to help with that.

decoupling django apps - best practice to layout a project

I'm working on a project that has several apps, and want to include a news app for news stories.
However, I'd like to link news stories to objects in my custom app, but use an open source news app to run the news.
At the moment I've simply hacked the chosen news app to add in a ForeignKey relationship with my model.
i.e. a widgets app, with a widget model
then a news app with the entry model linked directly to my widget model
Is there a better way to do this? because if I want to update the news app with the latest version of it, it'll obviously overwrite my hack.
I could have the link from my custom model, but the workflow should really be
Add news article
choose a widget to link it to
NOT
Add a news article, save
Find the widget to link it to
Link back to the news article
I think you could have a model which inherits from the model in the external app. Something along the lines of:
MyNewsArticle(ExternalAppNewsArticle):
object = models.ForeignKey(MyObject)
As long as you're adding things rather than dropping things, this should work. You have to be careful if the model from the external app has any Custom Managers declared though, because by default Django will not inherit them. You might need to declare them again in your own model.