Not Displaying error message in Django Admin? - python

Whenever i try to add project of same name it leads to IntegrityError at /admin instead there should be an Error message on Django admin that "This project already exists". Also i have two users. So same user cannot have project of same name and different users can have project of same name. Please let me know how to fix this.
class Project(models.Model):
name = models.CharField(max_length=200)
added_by = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, blank=True, null=True, default=None)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
unique_together = (("name", "added_by"),)

Related

Django user subscribe user relation

I`m creating a simple blog now, and the main problem is to create a relation between Users. I use a default django User which should subscribe another user who is an author of post.
I have only one Post model in my app
class Post(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
title = models.CharField(max_length=128)
content = models.TextField(blank=True)
created_on = models.DateTimeField(auto_now_add=True)
seen = models.ManyToManyField(User, related_name='blog_posts', blank=True)
The relationship you're referring to isn't about the Post model as I understand it. So I think it might be better if you create a separate model. I share a model below as an idea, you can edit field names or add/delete fields according to your needs.
class AuthorSubscription(models.Model):
author = models.OneToOneField(User, on_delete=models.CASCADE, 'author_subscription')
subscribers = models.ManyToManyField(User, related_name='subscriptions', blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

unique_together = (("name", "added_by"),) in Django model gives IntergretyError at/admin instead of Validation Error

I want to show a validation message like "This Project already exists" in my django admin whenever i create a project with same name.
I keep getting an IntegrityError at my Name. Isn't Django supposed to validate this and give an ValidationError if I use unique_together=((,)) in my model? Or do I have to Try and Catch the IntegrityError myself?
Also i have made two users and gave permissions to them from main admin user. so i want that one user1 cannot have projects with same name like proj1 while the user2 can have name of project as proj1.
Can anyone tell me a best practice for validating unique users inside a form/model.
models.py
class Project(models.Model):
name = models.CharField(max_length=200)
added_by = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, blank=True, null=True, default=None)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
unique_together = (("name", "added_by"),)
def __str__(self):
return self.name

Making field unique in Django Admin

I have a model
class ScrapEventInstructionMap(models.Model):
instruction = models.ForeignKey(Instruction)
scrap_code = models.CharField(max_length=100, null=True, blank=True)
event_code = models.CharField(max_length=100, null=True, blank=True)
class Meta:
unique_together = (("instruction", "event_code"), ("instruction", "scrap_code"))
I have used unique_together so that for a particular instruction, scrap_code and event_code should be unique.
And from frontend if we're clicking on a particular scrap_code and event_code the instruction page is opening.
I am using InlineModelAdmin in Admin.py so that for one instruction there can be multiple scrap_code or event_code
But issue is like we have two instructions, Instruction 1 and Instruction 2
So if I am entering same scrap_code or event_code in both instructions. It is saving.
I have to restrict it in admin page that event_code and scrap_code should also be unique for different instructions.
Try this
class ScrapEventInstructionMap(models.Model):
instruction = models.ForeignKey(Instruction)
scrap_code = models.CharField(max_length=100, null=True, blank=True)
event_code = models.CharField(max_length=100, null=True, blank=True)
class Meta:
unique_together = (("event_code", "scrap_code"))
You should add custom validation in BaseInlineFormSet of InlineAdmin. Here is example of BaseInlineFormSet validation.

Migrations being applied according to User in models.py instead of being applied according to migrations in the database

I have written a custom django migrations command as shown below
User = get_user_model()
def populate_asset_assignee(apps, schema_editor):
for user in User.objects.all():
user.save()
# Some more code
The user model looks as follows
class User(AbstractUser):
username = None
email = models.EmailField(max_length=50, unique=True)
cohort = models.IntegerField(blank=True, null=True)
slack_handle = models.CharField(max_length=50,
blank=True, null=True)
picture = models.CharField(max_length=255, blank=True, null=True)
phone_number = models.CharField(max_length=50, blank=True, null=True)
last_modified = models.DateTimeField(auto_now=True, editable=False)
password = models.CharField(max_length=128, blank=True, null=True)
location = models.ForeignKey('SomeCentre',
blank=False,
null=True,
on_delete=models.PROTECT)
# Some more fields
I added the location field recently and have the migrations for it which is applied after this custom migration has been applied. The problem I am having is that whenever I try to make the migrations on the test db or a new database, I get the error django.db.utils.ProgrammingError: column core_user.location_id does not exist which is being raised inside the populate_asset_assignee method when I try to do the user in User.objects.all()
Any ideas why location_id is beeing checked yet I haven't applied the migrations for the location field yet.

Django error filter on ManyToMany relation

I would have all message about the request.user
Consider this code:
views.py
conversation = MessageConversation.objects.filter(Q(user=request.user.id) | Q(recipient=request.user)).order_by ('-date_create')
models.py
class MessageConversation(models.Model):
close = models.BooleanField(default=False)
subject = models.CharField(max_length=32)
user = models.IntegerField(max_length=32, null=True, blank=True)
recipient = models.ManyToManyField(User, null=True, blank=True)
I want show all conversations regarding of user connected.If I have more than one entity in the ManyToMany relation the query is multiplied.

Categories

Resources