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.
Related
I am using python 3.8 and django 4.0.6 + drf 3.13.1
There are models
class Profile(models.Model):
user='US'
manufacturer = 'MA'
admin='AD'
choice=[
(user, 'User'),
(manufacturer, 'Manufacturer'),
(admin, 'Admin')
]
user_company = models.CharField(max_length=2, choices=choice)
user = models.OneToOneField(User, on_delete=models.CASCADE)
last_request = models.JSONField(null=True)
class ProfileCompany(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
company = models.OneToOneField('Company', on_delete=models.CASCADE)
classCompany(models.Model):
id_company = models.IntegerField(null=True, unique=True)
Company = models.CharField(max_length=128)
Direction = models.CharField(max_length=512, blank=True)
Description = models.TextField(null=True, blank=True)
Categories = ArrayField(base_field=models.CharField(max_length=128), null=True, blank=True)
Products = ArrayField(base_field=models.CharField(max_length=128), null=True, blank=True)
Serializer
class CompanySerializer(serializers.ModelSerializer):
class Meta:
model=Company
fields = '__all__'
The task is to make the pre-moderation of the creation and updating of companies by the admin.
Manufacturer creates a new company or updates data in it, this data is not visible to all users, but only to the admin. The admin accepts or rejects this data with a comment (in this case, the Manufacturer receives a message with this comment, corrects the data and sends the data again for moderation)
I could not connect django-moderation, because it is not suitable for REST.
Are there ready-made libraries or solutions?
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)
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"),)
i am trying to filter a data set based on a custom user model and having some difficulty with the data.
Basically, i have a registration form in which i am making user select the company they are associated with. So i have created a custom user model with a foreign key association to the company table.
Now, i am trying to query a second dataset so when user logs in, the application looks up the users company association and filters the data to only show results that are associated to the user's company choice.
any suggestion on how i can do this?
my user model is below:
class Account(AbstractBaseUser):
email = models.EmailField(verbose_name="email", max_length=60, unique=True)
username = models.CharField(max_length=30, unique=True)
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True, blank=True)
the table that i am trying to query on has model below:
class Order(models.Model):
customer = models.ForeignKey(Customer, on_delete= models.SET_NULL, null=True)
product = models.ForeignKey(Product, on_delete= models.SET_NULL, null=True)
date_created = models.DateTimeField(auto_now_add=True, null=True, blank=True)
requestorname = models.CharField(max_length=100, null=True)
requestorage = models.CharField(max_length=2,null=True, blank=True)
child_id = models.ForeignKey(ChildID, on_delete=models.SET_NULL, null=True, blank=True)
comments = models.CharField(max_length=100,null=True, blank=True)
requestdate_create = models.DateTimeField(auto_now_add=True)
note that both table has association to customer table using a foriegn key, so i want the user to only see the order associated to the company he/she belongs to.
appreciate any directions to help write the view. Thanks
So I was able to solve my own problem. I had to pass the request in as an argument. posting it here so folks with the same question can find answer. the view goes something like this.
def externalrequest(request):
args = request.user.customer_id
external = Order.objects.filter(customer=args)
context = {'external':external}
return render(request, 'accounts/external.html', context)
Model:
class Comment(models.Model, CharField, ListField):
user = models.ForeignKey('auth.User', on_delete=models.CASCADE, related_name='comment_user',
blank=True, null=True)
news = models.ForeignKey(News, related_name='comment_of', on_delete=models.CASCADE)
content = models.CharField(validators=[MinLengthValidator(4)], max_length=200")
parent_comment = models.ForeignKey('self', blank=True, null=True, related_name='parent',
on_delete=models.CASCADE)
class Meta:
ordering = ['-created']
def __str__(self):
return self.content
When I login in admin click comment,there is an error:
Direct assignment to the reverse side of a related set is prohibited. Use parent.set() instead.
When I remove:
parent_comment = models.ForeignKey('self', blank=True, null=True, related_name='parent',
on_delete=models.CASCADE)
The error gone.
So,how to modify parent_comment,I think the issue is here.