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.
Related
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.
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)
Hey guys I have this coupon model, I need to give it a remove functionality (not delete from database), to remove coupon from the order if the customer wishes to. How can I have that functionality? If I use delete(), it will delete the coupon from database, and using remove() shows an attribute error.
model:
class Coupon(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True)
code = models.CharField(max_length=20)
amount = models.FloatField(max_length=4)
valid_from = models.DateTimeField(null=True)
valid_to = models.DateTimeField(null=True)
max_value = models.IntegerField(validators=[MaxValueValidator(100)], verbose_name='Coupon Quantity', null=True)
used = models.IntegerField(default=0)
Thanks
Just unset relation on foreign key
order.coupon = None
order.save()
I have three models in my django app...a members model, an application model and an applications review model.
My members model looks like this...
class Members(models.Model):
TITLES = (
('chairman', 'Chairman'),
('secretary', 'Secretary')
)
user = models.OneToOneField(User, on_delete=models.CASCADE)
title = models.CharField(max_length=10, choices=TITLES, default='secretary')
My Applications model...
class Application(models.Model):
firstname = models.CharField(max_length=20)
middlename = models.CharField(max_length=20)
lastname = models.CharField(max_length=20)
dob = DateField()
The applications review model...
class ApplicationsReview(models.Model):
APPLICATION_STATUS = (
('pending', 'Pending Review'),
('approved', 'Approved'),
('rejected', 'Rejected')
)
applicant = models.OneToOneField(Application, on_delete=models.CASCADE, primary_key=True)
chairman = models.ForeignKey(Members, related_name='chairs', on_delete=models.CASCADE)
secretary = models.ForeignKey(Members, related_name='secretaries', on_delete=models.CASCADE)
application_status = models.CharField(max_length=10, choices=APPLICATION_STATUS, default='pending')
status_justification = models.TextField()
date = models.DateTimeField(auto_now_add=True)
When an application is created, I would like its review instantiated as well, hence, I have the following signal right below the applications review model...
# When an application is created, create with it an application review and associate it with the application instance
#receiver(post_save, sender=Application)
def create_application_review(sender, **kwargs):
instance = kwargs['instance']
created = kwargs['created']
if created:
ApplicationReview.objects.create(applicant=instance)
However, when I try to add an application in django admin I get the error
null value in column "chairman_id" violates not-null constraint
DETAIL: Failing row contains (3, pending, 2019-02-08 03:26:04.643452+00, null, null).
The error seems to be as a result of the signal trying to instantiate an ApplicationsReview instance without providing the values for the chairman and secretary. Even setting those to allow null fields doesn't get rid of the error. Is there something I'm missing here?
Creating ApplicationsReview requires you to pass the following details - chairman, secretary, status_justification But while creating ApplicationReview in the signal you are just passing value of applicant, So Django is assuming the values of chairman, secretary, status_justification fields as Null that is why you are getting this error.
If you want to make these field Non-compulsory you can pass null=True, Blank=True while defining the field in the model.
Something like this:
chairman = models.ForeignKey(Members, null=True, blank=True, related_name='chairs', on_delete=models.CASCADE)
# End
You can refer this answer to get more understanding of when to use null=True, blank=True or both.
https://stackoverflow.com/a/8609425/6280433
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()