I'm trying to render a user's direct messages and group them into threads in the template. Each message has a "thread" field to indicate the thread. How would I render the messages to the template, grouped into their thread? Here is my model:
class Thread(models.Model):
transaction = models.OneToOneField(
Transaction,
on_delete=models.CASCADE,
primary_key=True,
)
create_time = models.DateTimeField(default=timezone.now, null=True, blank=True)
class Message(models.Model):
thread = models.ForeignKey(Thread, on_delete=models.CASCADE)
sender = models.ForeignKey(get_user_model(), related_name='sender', on_delete=models.CASCADE)
reciever = models.ForeignKey(get_user_model(), related_name='reciever',on_delete=models.CASCADE)
text = models.TextField(max_length=4000)
create_time = models.DateTimeField(default=timezone.now, null=True, blank=True)
Any help is appreciated.
It looks like I need to use regroup in the template.
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)
I'm making a task tracker webapp (the full source code is also available) and I have a database structure where each task has a title, a description, and some number of instances, that can each be marked incomplete/incomplete:
class Task(models.Model):
title = OneLineTextField()
description = models.TextField(blank=True)
class TaskInstance(models.Model):
task = models.ForeignKey(Task, on_delete=models.CASCADE)
is_complete = models.BooleanField()
The task and the instances can be shared separately, although access to the instance should imply read access to the task. This is intended for classroom situations, where the teacher creates a task and assigns it to their students.
class TaskPermission(models.Model):
task = models.ForeignKey(Task, on_delete=models.CASCADE, related_name='permissions')
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='task_permissions_granted')
shared_by = models.ForeignKey(User, on_delete=models.PROTECT, null=True, related_name='task_permissions_granting')
can_edit = models.BooleanField(default=False)
class Meta:
unique_together = 'task', 'user', 'shared_by',
class TaskInstancePermission(models.Model):
task_instance = models.ForeignKey(TaskInstance, on_delete=models.CASCADE, related_name='permissions')
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='task_instance_permissions_granted')
shared_by = models.ForeignKey(User, on_delete=models.PROTECT, null=True, related_name='task_instance_permissions_granting')
can_edit = models.BooleanField(default=False)
class Meta:
unique_together = 'task_instance', 'user', 'shared_by',
My question is how to create a form for TaskInstances with fields for its is_complete, as well as its Task's title and description. Would something like this work? Or would I need to implement my own save and clean methods?
class TaskForm(ModelForm):
class Meta:
model = TaskInstance
fields = ('is_complete', 'task__title', 'task__description')
I think inlineformset_factory is what I'm looking for!
Actually, it does not seem to be useful: it is for multiple forms of the same type, not different types...
I got an error,ValueError at /app/recomment/1/
Cannot assign "": "ReComment.target" must be a "POST" instance.The error is happened when I put Recomment button.I wanna make a page which is shown comment&recomment.I wrote codes
in models.py
class POST(models.Model):
title = models.CharField(max_length=100)
created_at = models.DateTimeField(auto_now_add=True)
class Comment(models.Model):
name = models.CharField(max_length=100, blank=True)
target = models.ForeignKey(POST, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True, null=True)
class ReComment(models.Model):
name = models.CharField(max_length=100, blank=True)
target = models.ForeignKey(POST, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True, null=True)
I cannot understand the error message's meaning that is I should put POST into obj.target = comment in place of comment in views.py.The variable of comment is send via POST,so I think this is POST instance.I cannot understand the difference of POST instance and normal instance.What is wrong in my code?How should I fix this?
Do something like this and check that to not forget anything.
class DetailView(generic.DetailView):
model = POST <<<<-------- error give true model here
template_name = 'detail.html'
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.