Is it a good aproach to use Django signals to implement email notification system? I have CustomUser model related with CustomUserPreferences planned as follows:
class CustomUserPreferences(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, default=None, on_delete = models.CASCADE, primary_key = True)
lesson_notification = models.BooleanField(default=True)
journal_notification = models.BooleanField(default=False)
login_notification = models.BooleanField(default=False)
excercise_notification = models.BooleanField(default=False)
homework_notification = models.BooleanField(default=True)
class CustomUser(AbstractUser):
...
email = models.EmailField(_('email address'), unique=True)
preferences = models.OneToOneField(CustomUserPreferences, null = True ,default=None, on_delete = models.CASCADE)
students = models.ManyToManyField(to = 'self', related_name = 'teachers', symmetrical = False, blank = True)
Whenever a new object of lets say Lesson is created I want to send an email to the user and that's fine - becaouse it won't overload any server.
The question is: will it pay off to use signals for a list of users that contains lets say 100s or 1000s of users? I'm afraid it will slow down the whole application.
Is there any other "clear and elegant" way to do this? Django docs advices not to use signals whenever it's possible.
Related
i have such a model
class Summaries(models.Model):
user = models.OneToOneField(User, on_delete = models.CASCADE, primary_key = True)
father_name = models.CharField(max_length=60, blank=True)
myfile = models.FileField(upload_to="document")
class Summary_Createes(models.Model):
suser = models.ForeignKey(Summaries, on_delete = models.CASCADE)
for the active, current user
"Summary_Createes" The user who saved this model should only see it for himself
how can i filter such data i need filter
Summary_Createes.objects.filter(suser__user=request.user)
Hi Djangonauts,
I am new to Django so please forgive any errors in logic or code
I have a accounts app that has a Profile model with a field is_verified Now I have another app called verification. That has a model Verification and a field called verify I want to create a logic such that when you verify the user on the verification app. The is_verified on profile app is also marked as True
models.py for Profile
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
city = models.CharField(max_length=100)
country = models.CharField(max_length=100)
is_verified = models.BooleanField(default=False)
models.py for Verification
class Verification(models.Model):
user = models.ForeignKey(User, related_name='verified')
applied_on = models.DateTimeField(auto_now_add=True)
verify = models.BooleanField(default=False)
deny = models.BooleanField(default=False)
verified_on = models.DateTimeField()
denied_on = models.DateTimeField()
def verify_a_user(self, user):
self.verify = True
user.profile.is_verified = True
return user.profile.is_verified.save()
Is this correct? Is there a better way to execute this code
Take a look at https://docs.djangoproject.com/en/2.0/topics/signals/
Either send a pre_save or post_save signal from your Verification model.
https://docs.djangoproject.com/en/2.0/ref/signals/#django.db.models.signals.pre_save
https://docs.djangoproject.com/en/2.0/ref/signals/#django.db.models.signals.post_save
Then register the listener function in your Profile app.
For details and example
https://docs.djangoproject.com/en/2.0/topics/signals/#connecting-to-signals-sent-by-specific-senders
I wants to create user's private message application.
Message model would looks like this:
class Message(models.Model):
title = models.CharField(max_length = 60)
mssg_from = models.ForeignKey(User, related_name = 'message_from')
mssg_to = models.ForeignKey(User, related_name = 'message_to')
text = models.TextField()
reciver_deleted = BooleanField( default = False )
sender_deleted = BooleanField( default = False )
delivery_date = models.DateTimeField(auto_now = True, auto_now_add = False)'message_from')
I want to delete message from database only when both users deleted it.
I know how to do it in view (it's why I created boolean fields) but...
What if I delete a user? how to handle it?
I want messages to be still avaliable for an user even if secound user was deleted.
But if I delete an user and let message still exist then one of ForeignKey would refer to user that doesn't exist.
How to handle with this kind of situations?
You could modify your FK fields to:
mssg_from = models.ForeignKey(User, related_name = 'message_from', null=True, on_delete=models.SET_NULL)
mssg_to = models.ForeignKey(User, related_name = 'message_to', null=True, on_delete=models.SET_NULL)
then when you delete one of the users, the corresponding field will be set to NULL.
I have an Event model. Events can have many 'presenters'. But each presenter can either 1 of 2 different types of profiles. Profile1 and Profile2. How do I allow both profiles to go into presenters?
This will be 100% backend produced. As to say, admin will be selecting "presenters".
(Don't know if that matters or not).
class Profile1(models.Model):
user = models.ForeignKey(User, null=True, unique=True)
first_name = models.CharField(max_length=20, null=True, blank=True)
last_name = models.CharField(max_length=20, null=True, blank=True)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
about = models.TextField(null=True, blank=True)
tags = models.ManyToManyField(Tag, null=True, blank=True)
country = CountryField()
avatar = models.ImageField(upload_to='avatars/users/', null=True, blank=True)
score = models.FloatField(default=0.0, null=False, blank=True)
organization = models.CharField(max_length=2, choices=organizations)
class Profile2(models.Model):
user = models.ForeignKey(User, null=True, unique=True)
first_name = models.CharField(max_length=20, null=True, blank=True)
last_name = models.CharField(max_length=20, null=True, blank=True)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
about = models.TextField(null=True, blank=True)
tags = models.ManyToManyField(Tag, null=True, blank=True)
country = CountryField()
avatar = models.ImageField(upload_to='avatars/users/', null=True, blank=True)
score = models.FloatField(default=0.0, null=False, blank=True)
...
class Event(models.Model):
title = models.CharField(max_length=200)
sub_heading = models.CharField(max_length=200)
presenters = ManyToManyField(Profile1, Profile2, blank=True, null=True) ?
...
# I've also tried:
profile1_presenters = models.ManyToManyField(Profile1, null=True, blank=True)
profile2_presenters = models.ManyToManyField(Profile2, null=True, blank=True)
# is there a better way to accomplish this?...
I think you have a desing problem here. In my opinion, you must think what is a Presenter and what's the different between a Presenter with "profile 1" and with "profile 2". What are you going to do with this models? Are you sure there are just two profiles? Is there any chance that, in some time from now, a different profile ("profile 3") appears? And profile 4? and profile N?
I recommend you to think again about your models and their relations. Do NOT make this decision thinking of how difficul/easy will be to handle these models from django admin. That's another problem and i'll bet that if you think your models a little bit, this won't be an issue later.
Nevertheless, i can give you some advice of how to acomplish what you want (or i hope so). Once you have think abount how to model these relations, start thinking on how are you going to write your models in django. Here are some questions you will have to answer to yourself:
Do you need one different table (if you are going to use SQL) per profile?
If you cannot answer that, try to answer these:
1) What's the difference between two different profiles?
2) Are there more than one profile?
3) Each presenter have just one profile? What are the chances that this property changes in near future?
I don't know a lot about what you need but i think the best option is to have a model "Profile" apart of your "Presenter" model. May be something like:
class Profile(models.Model):
first_profile_field = ...
second_profile_field = ...
# Each presenter have one profile. One profile can "represent"
# to none or more presenters
class Presenter(models.Model):
first_presenter_field = ....
second_presenter_field = ....
profile = models.ForeignKey(Profile)
class Event(models.Model):
presenters = models.ManyToManyField(Presenter)
....
This is just an idea of how i imagine you could design your model. Here are some links that may help you once you have design your models correctly and have answered the questions i made to you:
https://docs.djangoproject.com/en/dev/topics/db/models/#model-inheritance
https://docs.djangoproject.com/en/dev/misc/design-philosophies/#models
http://www.martinfowler.com/eaaCatalog/activeRecord.html
And to work with the admin once you decide how your design will be:
https://docs.djangoproject.com/en/dev/ref/contrib/admin/
EDIT:
If i'm not wrong, the only difference between profile 1 and 2 fields is the "organization" field. Am i right? So i recommend you to merge both models since they are almost the same. If they have different methods, or you want to add different managers or whatever, you can use the proxy option of django models. For example, you can do this:
class Profile(models.Model):
#All the fields you listed above, including the "organization" field
class GoldenProfile(models.Model):
#you can define its own managers
objects = GoldenProfileManager()
....
class Meta:
proxy = True
class SilverProfile(models.Model):
....
class Meta:
proxy = True
This way, you can define different methods or the same method with a different behaviour in each model. You can give them their own managers, etcetera.
And the event class should stay like this:
class Event(models.Model):
title = models.CharField(max_length=200)
sub_heading = models.CharField(max_length=200)
presenters = ManyToManyField(Profile, blank=True, null=True)
Hope it helps!
I have a model like this:
class database(models.Model):
db_name = models.CharField('Name', max_length=20)
server = models.ForeignKey(dbServer, unique=True)
user = models.ForeignKey(User)
In my view I want to grab every database realated to the current user (has to be logged in at that point).
I'm sure there is an easy way to do this, but I can't find it.
You could simply use:
some_user.database_set.all()
How ever I recommend trying related_name attribute, for example:
class database(models.Model):
db_name = models.CharField('Name', max_length=20)
server = models.ForeignKey(dbServer, unique=True)
user = models.ForeignKey(User, related_name="databases")
And then:
some_user.databases.all()
Use the backwards relationship:
databases = User.database_set.all()