Creating an employee for each user record - django

I want to create a record in employee model each time I create an user. Any suggestions about how to do that?

If you mean the Django-supplied User model, then probably best to use pre-save or post-save signals.
If you mean your own model, you can subclass its save method.

Related

Sending emails after updating the model from the admin page (Django)

Hello I want to know if there is a way to send an email to a user upon someone updating certain fields in the Django admin page being updated. Is there a way to do this? I already have an email being sent upon the forms completion, but I need to send more emails once one of the users updates through the admin page.
I have taken a look at the post_save, model_save and save_formset methods, but I did not feel that they were what I was looking for.
Try overriding the ModelAdmin.save_model method. I think it has hooks for all the information you require.
The change variable lets you distinguish between a user adding or changing the model instance.
form.changed_data gives you a list of the names of the fields which have changed, which lets you determine whether or not to send the email.
Finally request.user identifies the user which made the changes.
You need django.db.models.signals.post_save signal. It is sanding after the model has been saved.
def my_callback(sender, **kwargs):
# Your specific logic here
pass
post_syncdb.connect(my_callback, sender=yourapp.models.TheModel)
Arguments sent with this signal:
sender:
The model class.
instance:
The actual instance being saved.
created
A boolean; True if a new record was created.
raw:
A boolean; True if the model is saved exactly as presented (i.e. when loading a fixture). One should not query/modify other records in the database as the database might not be in a consistent state yet.
So you need only callback and sender.

Django logged in user in models.py or signal handler

Is there a way to access the request.user in either models.py or in a signal handler?
I'm using the m2m_changed signal and defining it in my models.py - I'd like to access the logged in user there.
Is there a way to do this?
I'm assuming user making a change is not necessarily record owner or author. This means model lookups are useless and you need to pass this data via signal.
Good way to do this is to create custom signal which has current user as one of attributes and emit it in view code when the data is being saved.

Should I ForeignKey to Django User or a Profile model?

I'm wondering what people's thoughts are on joining models directly to the auth.User object vs to the user's profile model.
I'm storing some different types of models which my user are adding in my app. App users will search for other users via criteria on these models.
On the one hand, I'm thinking that if I join straight to User then I won't need to do request.user.get_profile() each time I need to grab the User's records, and it doesn't presuppose that a User always has a profile (they do in my app at the mo, but still). This leaves the profile model as just containing the user's contact details.
On the other hand, I imagine I'll most likely need values from the Profile (eg name, location) when I'm looking up these other models.
No doubt either will work, so maybe it doesn't matter, but I just wondered what other people's thoughts were.
Thanks!
Ludo.
I would also recommend creating foreign-keys to the User model. It just makes your life simpler when working with the user object in the view, for one. So, you can do things like request.user.foo_set, etc. without having to go through the profile model.
In general: If you want to make your apps reusable, always create foreign keys to User model.
As you already said, in most cases you will need User as well as Profile instance, so to prevent multiple database queries, use cache.
If reusability isn't relevant, create foreign key to Profile and use select_related() to get User instance with single query.

Is it okay to use django's auth_user model for storing user's info

If I am trying to create a login app for my project is it advisable to use auth_user as the model to store newly created user's in and just extend in in my signup app through:
from auth.models import User
class UserForm(ModelForm):
class Meta:
model=User
Or is it better to create a custom model to store users in. Will what I have above even work? I'm sure the answer is either "of course, that's what it's there for" or "dear god why would you want to do that" but I don't know which.
User is meant to store the credentials of users that will be able to log into and use the system via normal Django auth mechanisms. If your project will use Django's auth then yes, User is the correct model to use.
The short answer is yes. and if you want to store additional information of the user, you could create another model and a foreign key to the user model.

Integrate Django Models with a User Account

Does anyone have a list of things they do to associate some model with a user?
I.e. if a blog entry has a user, you have to do a few things:
- Add a foriegnkey field
- Make the foreign key field editable = False
- On save/load make sure that request.user matches entry.user
This is what I could come up with. Is there an easier way to do this? Anything else to keep in mind?
It may make more sense to associate the model with a user Profile object than to associate directly with User. That way it's easier to connect the model to custom attributes of the user.
That is an appropriate way, although it may be useful to have it editable. The user should be assigned in the view, not pushed through to the model, in order to maintain clean MVP separation.