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?
Related
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)
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)
I have 2 models in Django that are connected by a one to one relationship. For example:
class User(AbstractBaseUser):
username = models.CharField(max_length=15, unique=True)
email = models.EmailField(max_length=100, unique=True)
date_joined = models.DateTimeField(auto_now_add=True,
null=True)
bio = models.CharField(max_length=200, null=True)
avatar = models.CharField(max_length=200, null=True)
profile = models.OneToOneField(Settings, null=True)
class Profile(models.Model):
GENDER_CHOICES = (
('M', 'Male'),
('F', 'Female'),
)
bio = models.CharField(max_length=200, null=True)
avatar = models.CharField(max_length=200, null=True)
gender = models.CharField(max_length=1, choices=GENDER_CHOICES, null=True)
dob = models.DateField(max_length=8, null=True)
Every user may have Profile details, but some users may not have entered any information so they might not have any details provided yet (0 rows). What I'd like to do is create a settings page that either creates or updates the user profile details upon submission. Basically this is what I have so far:
class ProfileSettings(UpdateView):
template_name = 'oauth/profile-settings.html'
form_class = ProfileForm
model = Profile
def get_object(self, queryset=None):
return get_object_or_404(Profile, user=self.request.user)
With this type of view, the update works when a user has profile details, but when the user doesn't have profile details a 404 error is displayed instead. Is it possible to display the view to the user even though he doesn't have profile details and if so, could they create profile details for themselves and then can go back and update them?
I have the following two models in Django. One is basically an extension of the base Django user class and the other is a company model. I want to say that a user can belong to one or more companies and that a company can also have one or more contacts = "Users". Would this be a correct setup? How should I represent the tie between user and company?
User Profile model:
class Profile(models.Model):
user = models.OneToOneField(User)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
Company model:
class Company(models.Model):
name = models.CharField(max_length=120)
account_name = models.CharField(max_length=10, default="")
sales_rep = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_sales", default="")
csr = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_csr", default="")
class CompanyContact(models.Model):
name = models.CharField(max_length=40, default="")
email = models.CharField(max_length=50, default="")
user = models.ForeignKey(User)
company = models.ForeignKey(Company)
First, is there a reason to extend the User model? The default model already includes a first_name and last_name field, so you don't need an additional model just for that data. Similarly, you don't really need CompanyContact because the User model also contains email and name (again, through first_name and last_name) fields.
You can add in your contacts as a ManyToManyField. If you want to use the custom Profile model instead of User, just replace User (in the ManyToManyField) with Profile.
class Company(models.Model):
name = models.CharField(max_length=120)
account_name = models.CharField(max_length=10, default="")
sales_rep = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_sales", default="")
csr = models.ForeignKey(User, related_name="%(app_label)s_%(class)s_csr", default="")
contacts = models.ManyToManyField(User) # or Profile
This allows each company to have many contacts and each user to be a contact of many companies – thus many-to-many.
Now, if you wanted extra data to describe the many-to-many relationship, you can have another model for that. For example, you may want to keep a record if the contact is still active or what their role is. So, you may have a CompanyContact model that is similar to:
class CompanyContact(models.Model):
active = models.BooleanField(default=False)
role = models.CharField(max_length=50, default="")
user = models.ForeignKey(User) # or Profile
company = models.ForeignKey(Company)
Then, declare the ManyToManyField relationship to use this new model:
class Company(models.Model):
...
contacts = models.ManyToManyField(User, through="CompanyContact")
# or contacts = models.ManyToManyField(Profile, through="CompanyContact")
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.