I need to add data to the database For that, I'm trying some code. But I can't add foreignkey values. The code throws exceptions. Here are my views.py, models.py code and exceptions.
first try:
views.py
def notification(request):
user = request.user
if request.method == 'POST':
property_id = request.POST['property_id']
owner = request.POST['owner_id']
property_object = Property.objects.get(id=property_id)
owner_object = Property.objects.get(owner=owner)
notification = user, "have intrested in your property"
property_object.notify.add(user)
notifications = Notifications.objects.create(notification=notification, owner=owner_object
,property=property_object)
notifications.save()
it throws exception
ValueError: Field 'id' expected a number but got 'hafis'.
second try
views.py
def notification(request):
user = request.user
if request.method == 'POST':
property_id = request.POST['property_id']
owner = request.POST['owner_id']
property_object = Property.objects.get(id=property_id)
notification = user, "have intrested in your property"
property_object.notify.add(user)
notifications = Notifications.objects.create(notification=notification, owner=owner,
property=property_id)
notifications.save()
it throws exception
ValueError: Cannot assign "'hafis'": "Notifications.owner" must be a "User" instance.
models.py
class Property(models.Model):
owner = models.ForeignKey(User, null=True, blank=True, on_delete=models.CASCADE)
headline = models.CharField(max_length=255)
city = models.CharField(max_length=100)
location = models.CharField(max_length=100)
facilites = models.TextField()
rent = models.CharField(max_length=200)
images = models.FileField(upload_to='media/uploads',null=True)
email = models.EmailField()
mobile = models.IntegerField(null=True)
date = models.DateTimeField(auto_now_add=True)
notify = models.ManyToManyField(User, default=None, blank=True, related_name='Liked')
def __str__(self):
return self.headline
class Notifications(models.Model):
owner = models.ForeignKey(User, null=True, blank=True,on_delete=models.CASCADE)
property = models.ForeignKey(Property, null=True, blank=True,on_delete=models.CASCADE)
notification = models.TextField()
date = models.DateTimeField(auto_now_add=True)
def __str_(self):
return self.notification
HTML code
{% if accomodations %}
{% for accomodation in accomodations %}
<form action="{% url 'notification' %}" method="post">
{% csrf_token %}
<input type="hidden" name="owner_id" value="{{ accomodation.owner }}">
<button id="request-btn" name="property_id" value="{{ accomodation.id }}" class="btn btn-primary">Contact info:</button>
</form>
You need to pass the users instance. You can get user by this line of code
owner = User.objects.all().get(username=owner)
Note:
Every user should have differnt usernames
ValueError: Cannot assign "'hafis'": "Notifications.owner" must be a "User" instance. you're just passing in the ID but it requires it to be a User object.
def notification(request):
user = request.user
if request.method == 'POST':
property_id = request.POST['property_id']
owner = User.objects.get(id=request.POST['owner_id'])
property_object = Property.objects.get(id=property_id)
notification = user, "have intrested in your property"
property_object.notify.add(user)
notifications = Notifications.objects.create(notification=notification, owner=owner,
property=property_id)
notifications.save()
Related
I'm a little new to Django, so my question may be basic, but bare with me. Let's say I'm sharing my posts with some of my friends (who are manytomany relation with the post), but also giving an extra condition for whether they can comment on it, or not. This condition, together with the name of the user to be shared with are submitted through a forum. Now my problem is that I don't know how/where to save this condition. I will show you some of my code.
Models.py
class Task(models.Model):
name = models.CharField(max_length=50)
text = models.TextField()
deadline = models.DateTimeField()
created = models.DateField(auto_now_add=True)
updated = models.DateField(auto_now=True)
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='taskset')
shared = models.ManyToManyField(User, blank=True, related_name = 'shared_list')
def __str__(self):
return f"{self.name}-{self.user.username}"
class Comment(models.Model):
text = models.TextField()
task = models.ForeignKey(Task, on_delete=models.CASCADE, related_name='comments')
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='comments')
created = models.DateField(auto_now_add=True)
def __str__(self):
return f"{self.user.username}-{self.task}"
Share form html
{% extends 'base.html'%}
{% load crispy_forms_tags %}
{% block content %}
<h3>Enter the Username of the Friend you want to share with</h3>
<form method="POST">
{% csrf_token %}
{{form|crispy}}
<input type="submit", value="Share">
</form>
{% endblock content %}
And the view processing it
def share(request, id):
task = Task.objects.get(id = id)
if request.method == 'POST':
share_with = User.objects.get(username = request.POST['username'])
if share_with is not None:
task.shared.add(share_with)
task.save()
return redirect('index')
else:
form = ShareForm()
return render(request, 'share.html', {'form':form})
Thanks a lot, I've been stuck for two hours, PLEASE HELP!
I have a view with a list of tasks and tried to do simple task allocation on click but it is not working. Could you please support? I see in my console sth like that:
"POST /task/allocate HTTP/1.1" 302 0.
Thank you.
L
model:
class Task(models.Model):
name = models.CharField(max_length=248)
note = models.TextField(blank=True, null=True)
priority = models.NullBooleanField(blank=True, null=True)
status = models.IntegerField(choices=TASK_STATUS, default=TASK_STATUS[0][0])
placed_date = models.DateField(auto_now=True)
due_date = models.DateTimeField(blank=True, null=True)
completed = models.BooleanField(default=False)
completed_date = models.DateField(blank=True, null=True)
created_by = models.ForeignKey(
User,
null=True,
blank=True,
related_name="hana_created_by",
on_delete=models.CASCADE)
assigned_to = models.ForeignKey(User,
null=True,
blank=True,
related_name="hana_assigned_to",
on_delete=models.CASCADE)
urls:
path('excel_table', ex_views.ExcelTableView.as_view(), name = "excel-table"),
path("task/add", ex_views.TaskAddView.as_view(), name="task-add"),
path("task/<int:task_id>/", ex_views.TaskDetailView.as_view(), name="task-detail"),
path("task/<int:pk>/edit", ex_views.TaskEditView.as_view(), name="task-update"),
path("task/allocate", views.task_allocator, name="task-allocate"),
views:
class TaskAllocateView(View):
def post(self, request):
if request.POST.get("task_allocate") is not None:
tasks = Task.objects.filter(assigned_to=None)
for task in tasks:
task.assigned_to = random.choice(User.objects.all())
task.status = 1
task.save()
current_site = get_current_site(request)
subject = render_to_string('email/assigned_subject.txt', {"task": task})
body = render_to_string('email/assigned_body.txt', {
'task': task,
'site': current_site,
})
to_email = task.assigned_to.email
email = EmailMessage(subject, body, to=[to_email])
email.send()
if tasks:
messages.success(request, "Tasks succesfully allocated to your employees. Check status!")
messages.success(request, ('Notification email has been sent to assignees!'))
else:
messages.warning(request, "All tasks already allocated!")
messages.warning(request, ('Notification email already sent!'))
return redirect(reverse("excel-table"))
return redirect(reverse("excel-table"))
template:
<form method="POST" action="{% url "task-allocate" %}" role="form" class="d-inline">
{% csrf_token %}
<div style="display:inline;">
<button class="btn btn-info btn-sm" type="submit" name="task_allocate">
Task Allocate
</button>
</div>
</form>
if t.assigned_to != None:
t.status = 1
t.save()
messages.success(request, "Tasks succesfully allocated to your employees. Check status!")
return redirect(reverse("excel-table"))
this if will never be executed because created_by will remain Non until you save it.
t.assigned_to = random.choice(User.objects.all())
if t.assigned_to != None:
you did this thinking that this t.assigned_to is not None anymore, well it remains None because you didnt save.
i hope this one will help you.
I am using custom user authentication for verifying user. I have table named voter and I am tacking username and password via from and matching that with username and password of table if it match user will be logged in and allowed them to pass to next page which contain form. In that form I initiated data but it will not appear automatically it will appear only when i refresh the page
code of my files is bellow (for some big file only relevant code is included that's why it is partial)
model.py (partial)
class Voter(models.Model):
serial_voter_id = models.AutoField(primary_key=True)
voter_id = models.CharField(unique=True, max_length=10)
voter_name = models.CharField(max_length=255)
voter_constituency = models.ForeignKey(Constituency, models.DO_NOTHING, blank=True, null=True)
username = models.CharField(unique=True, max_length=32)
password = models.TextField()
voter_address = models.CharField(max_length=255, blank=True, null=True)
area = models.CharField(max_length=10, blank=True, null=True)
city = models.CharField(max_length=10, blank=True, null=True)
pincode = models.IntegerField(blank=True, null=True)
adhar_no = models.BigIntegerField(unique=True)
birth_date = models.DateField()
age = models.IntegerField()
fingerprint = models.TextField(blank=True, null=True)
authenticity = models.CharField(max_length=3, blank=True, null=True)
wallet_id = models.TextField()
class Meta:
managed = False
db_table = 'voter'
forms.py
from django import forms
from .models import Voter
class VoterForm(forms.ModelForm):
class Meta:
model = Voter
fields = [
'serial_voter_id',
'voter_id',
'voter_name',
'voter_constituency',
'username',
'voter_address',
'area',
'city',
'pincode',
'adhar_no',
'birth_date',
'age',
'authenticity',
'wallet_id'
]
views.py (partial)
from .models import Voter
from .forms import VoterForm
def voter_login(request, *args, **kwargs):
contex = {}
return render(request, "poll/voter_login.html", contex)
def voter_verification(request, *args, **kwargs):
f_username = request.POST.get('username')
voter = Voter.objects.get(voter_id=1) # thing need to be dynamic hear by replacing it with username
f_username = request.POST.get('username')
f_password = request.POST.get('password')
u_password = voter.password # fetching the password from voter object
u_password = u_password.decode() # decoding binary password to string
form = VoterForm(request.POST or None, instance=voter)
if form.is_valid():
form.save()
form = VoterForm()
contex = {
'voter' : voter,
'f_username' : f_username,
'f_password' : f_password,
'u_password' : u_password,
'form' : form
}
# compare hear username and password entered by user and from database if these are correcgt then allow this view or redirect to voter_login view
if voter.username == f_username and u_password == f_password:
return render(request, "poll/voter_verification.html", contex)
else:
return render(request, "poll/voter_login.html", {})
voter_login.html
{% extends 'header.html' %}
{% block content %}
<table>
<form method="get" action='voter_verification'> {% csrf_token %}
username <input type="text" name="username">
password <input type="password" name="password">
<input type="submit" name="login" value="login">
</form>
{% endblock %}
voter_verification.html (template file)
<!DOCTYPE html>
<html>
<body>
{% if f_username == voter.username and f_password == u_password %}
<h1>verified</h1>
{% else %}
<h1>wrong id and password</h1>
{% endif %}
<form method='post' action='vote_page'> {% csrf_token %}
{{ form.as_p }}
<input type='submit' value="sumbit">
</form>
</body>
</html>
changes query in view model ( def voter_verification(request, *args, **kwargs):)
def voter_verification(request, *args, **kwargs):
f_username = request.POST.get('username')
voter = Voter.objects.get(username=f_username)
then request user check in voter table which have exits with same user name.
Thanks to DrMaxwellEdison from reddit for providing answer
https://www.reddit.com/r/djangolearning/comments/fecn1f/question_django_data_in_form_only_appear_after/
Please do not have a separate model that stores usernames and passwords aside from your User model. You can simply add a OneToOneField to the User model to connect this Voter model to the authenticated User, and you don't need to do any extra data storage (plus, you are more likely to be handling the password information incorrectly, exposing it to being compromised if your site is breached).
Also per your comment on SO, the POST is not working because of CSRF protection (the traceback of your error would likely have indicated this already). Please see those docs for details on how to proceed (hint: do not disable CSRF!).
it's been a few hours since I tried to retrieve a list of users with the information of an intermediate table.
So I have a workspace model that is a manytomanyfield with users
There is also an intermediary table to differentiate the classic users and the workspace manager
I would like to display the list of users and add a small icon symbolizing the managers in the list.
But unfortunately it seems difficult for Django, to display both the list of users of the workspace with the information of the intermediate table.
In any case I look at the documentation of Django I have not managed to find how to do.
models.py
class Workspace(models.Model):
name = models.CharField(max_length=250, verbose_name="Nom du workspace")
members = models.ManyToManyField(User, through='Membership', verbose_name="Membres du workspace")
token = models.CharField(max_length=500) # token statique
join_token = models.CharField(max_length=500) # token dynamique
join_token_date = models.DateTimeField(auto_now_add=False, null=True, blank=True)
payday = models.DateField(max_length=10, verbose_name="Jour de paye", null=True, blank=True)
planning_image = ProcessedImageField(upload_to='planning',
null=True,
blank=True,
processors=[ResizeToFill(1299, 937)],
format='JPEG',
options={'quality': 100})
planning_thumbnail = ImageSpecField(source='planning_image',
processors=[ResizeToFill(280, 202)],
format='JPEG',
options={'quality': 100})
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('create-workspace')
class Membership(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
workspace = models.ForeignKey(Workspace, on_delete=models.CASCADE)
is_manager = models.BooleanField(default=False)
date_joined = models.DateTimeField(auto_now_add=True)
views.py
#login_required
def workspace_detail(request, token):
ins_workspace = get_object_or_404(Workspace, token=token)
list_members = ins_workspace.members.all()
for member in list_members:
if member == request.user:
current_user = Membership.objects.get(workspace=ins_workspace, user=request.user)
context = {
'name': ins_workspace.name,
'token': ins_workspace.token,
'list_members': list_members,
'payday': ins_workspace.payday,
'is_manager': current_user.is_manager,
}
return render(request, 'workspace/workspace_detail.html', context)
else:
return HttpResponseForbidden()
template.html
{% for item in list_members %}
{{ item.username }}
{% endfor %}
This is what I want:
template.html
{% for item in list_members %}
{% item.is_manager %}
{{ item.username }} (♔)
{% else %}
{{ item.username }}
{% endfor %}
You can do it like this:
Update Membership model with related name:
class Membership(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="membership")
workspace = models.ForeignKey(Workspace, on_delete=models.CASCADE)
is_manager = models.BooleanField(default=False)
date_joined = models.DateTimeField(auto_now_add=True)
Then you can update your view like following:
from django.db.models import F
#login_required
def workspace_detail(request, token):
ins_workspace = get_object_or_404(Workspace, token=token)
list_members = ins_workspace.members.all().annotate(is_manager=F('membership__is_manager'))
context = {
'name': ins_workspace.name,
'token': ins_workspace.token,
'list_members': list_members,
'payday': ins_workspace.payday,
'is_manager': request.user.membership.get(workspace=ins_workspace).is_manager,
}
return render(request, 'workspace/workspace_detail.html', context)
That should do the trick.
Here what I have done is that, I am using a reverse relation to get is_manager value from membership model. I am annotating that value in the queryset using F.
I'm trying to implement a draft function on my simple message application which using a custom UserModel
A draft function is when the user doesn't send the mail straight away but saves it.
I'm having trouble figuring out how can I implement a draft function using my model.
I am able to figure out how to send a mail.
#login_required
def Create(request):
person = Person.objects.get(user=request.user)
form = NewMessageForm()
if request.POST.get('send', False):
form = NewMessageForm(request.POST)
if form.is_valid():
recipient = form.cleaned_data['recipient']
subject = form.cleaned_data['subject']
message = form.cleaned_data['message']
thread = Thread.objects.create(subject=subject,user=request.user)
recipient = User.objects.get(username=recipient)
message = Message.objects.create(user=request.user,recipient=recipient,body=message,thread=thread)
return HttpResponseRedirect(reverse('world:message'))
elif request.POST.get('save', False):
#How can I save it as a draft?
return render(request,'create.html',{'messages':messages,'form':form,'person':person})
models
class Thread(models.Model):
subject = models.CharField(max_length=100, blank=True)
user = models.ForeignKey(User)
class Message(models.Model):
user = models.ForeignKey(User, related_name='sender')
recipient = models.ForeignKey(User, related_name='recipient')
created = models.DateTimeField(auto_now_add=True)
body = models.CharField(max_length=1000)
read = models.BooleanField(default=False)
trash = models.BooleanField(default=False)
sentmessage = models.BooleanField(default=False)
thread = models.ForeignKey(Thread)
def __unicode__(self):
return self.body
forms
class NewMessageForm(forms.ModelForm):
recipient = forms.CharField(required=True,max_length=1)
message = forms.CharField(widget=forms.Textarea,required=True,max_length=1)
checkbox = forms.BooleanField(required=False)
def clean_recipient(self):
recipient = self.cleaned_data['recipient']
try:
recipient = User.objects.get(username=recipient)
except User.DoesNotExist:
raise forms.ValidationError("This username does not exist")
return recipient
class Meta:
model = Thread
fields = ('subject',)
template
<form method="POST" >
{% csrf_token %}
{{form.recipient}}
{{form.subject}}
{{form.message}}
<input type="submit" value="send" name="_send" />
<input type="submit" value="save" name="_save" />
</form>
You could add a BooleanField "draft" in Message which determine if the current message is a draft or not and filter the mailbox by exclude every message with the state draft and where recipient = current_user.
This needs to modify a bit your logic by set draft to 0 when the message is sent, 1 when the message is save.
EDIT: I didn't see your messagesent attribute. You could use it for that comportement, I think it's appropriate :)