Please I have a project where I want to hide some fields in a form when the category selected belongs to a particular product type. The type of products is Single and Bundle products. So for instance, if I choose something like pens(Bundle) in the form category I should only see quantity in the form fields but if I select something like Desk(single) all fields should be available to fill. How do I implement this in Django? Thank you
My Model
TYPE =(('Single', 'Single'),('Bundle','Bundle'))
class Category(models.Model):
name = models.CharField(max_length=50, blank=True, null=True)
pro_type = models.CharField(max_length=50, choices=TYPE, null=True)
timestamp = models.DateTimeField(auto_now_add=False, auto_now=True, null=True)
def __str__(self):
return f'{self.name}'
class Product(models.Model):
pro_name = models.CharField(max_length=100, blank=True, null=True)
category = models.ForeignKey(Category, on_delete=models.CASCADE, blank=True, null=True)
quantity = models.IntegerField(default='0', blank=True, null=True)
issue_to = models.ForeignKey('Order',default='', on_delete=models.CASCADE,blank=True, null=True)
serial_num = models.CharField(max_length=100, blank=True, null=True)
model_num = models.CharField(max_length=100, blank=True, null=True)
storage_size = models.CharField(max_length=50, blank=True, null=True)
My views
def add_products(request):
form = ProductCreateForm(request.POST)
if request.method == 'POST':
if form.is_valid():
obj = form.save(commit=False)
obj.staff = request.user
obj.save()
return redirect('dashboard-products')
else:
form = ProductCreateForm()
context = {
'form': form,
}
I have been trying to build a search functionality in my app but i have stuck on querying for the foreign key field, as it doesn't return anything and the code shows no error. Below is my code.
forms.py
class StockSearchForm(forms.ModelForm):
class Meta:
model = Stock
fields = ['category', 'item_name']
My view where i implemented the search
views.py
def list_items(request):
header = 'List of items'
form = StockSearchForm(request.POST or None)
queryset = Stock.objects.all()
context = {
"form": form,
"header": header,
"queryset": queryset,
}
#Searching an item and category
if request.method == 'POST':
queryset = Stock.objects.filter(category__name__icontains=form['category'].value(),
item_name__icontains=form['item_name'].value()
)
context = {
"form": form,
"header": header,
"queryset": queryset,
}
return render(request, "list_items.html", context)
My models are as follows.
models.py
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=50, blank=True, null=True)
def __str__(self):
return self.name
class Stock(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
#category = models.CharField(max_length=50, blank=True, null=True)
item_name = models.CharField(max_length=50, blank=True, null=True)
quantity = models.IntegerField(default='0', blank=True, null=True)
receive_quantity = models.IntegerField(default='0', blank=True, null=True)
receive_by = models.CharField(max_length=50, blank=True, null=True)
issue_quantity = models.IntegerField(default='0', blank=True, null=True)
issue_by = models.CharField(max_length=50, blank=True, null=True)
issue_to = models.CharField(max_length=50, blank=True, null=True)
phone_number = models.CharField(max_length=50, blank=True, null=True)
created_by = models.CharField(max_length=50, blank=True, null=True)
reorder_level = models.IntegerField(default='0', blank=True, null=True)
timestamp = models.DateTimeField(auto_now_add=False, auto_now=True)
last_updated = models.DateTimeField(auto_now_add=True, auto_now=False)
export_to_CSV = models.BooleanField(default=False)
def __str__(self):
return self.item_name + '' + str(self.quantity)
So what happens is, I can search just fine the "item_name" field and results come up as required, but when i attempt to search for category no error pops up but no results show up, i kinda feel it's due to some foreign key fields issues but i can't just figure it out, I will much appreciate some help, this thing has been a nightmare for quite a while.
Try doing the following. I assume the form is not being properly used.
if request.method == 'POST' and form.is_valid():
queryset = Stock.objects.filter(category__name__icontains=form.cleaned_data.get('category'),
item_name__icontains=form.cleaned_data.get('item_name')
)
Try this:
queryset=Stock.objects.filter(category__name__icontains=form['category'].value(),
item_name__icontains=form['item_name'].value())
Hello everyone I'm trying top build a task manager web app using django, I need to assign task to one or multiple users I'm using manytomany relation in models.py and in views.py I'm adding created_by user automatically.
My problem is that when I do that I see that no users selected in assigned users but if I add created by user from the form it worked well.
class Task(models.Model):
task_id = models.AutoField(primary_key=True)
shortcode = models.CharField(max_length=15, unique=True, blank=True, null=True)
task_name = models.CharField(max_length=200)
task_progress = models.ForeignKey(TaskProgressStatus, on_delete=models.CASCADE, blank=True, null=True)
customer_name = models.ForeignKey(Customer, on_delete=models.CASCADE, blank=True, null=True)
task_priority = models.ForeignKey(TaskPriority, on_delete=models.CASCADE)
assigned_to_employee = models.ManyToManyField(User)
paid = models.BooleanField(default=False)
on_account = models.BooleanField(default=False)
currency = models.ForeignKey(Currency, on_delete=models.CASCADE, blank=True, null=True)
net_amount = models.DecimalField(decimal_places=2, max_digits=20, blank=True, null=True)
vat = models.IntegerField(default=11)
quote_validity = models.CharField(max_length=200, default='1 Month from offer date')
delivered = models.BooleanField(default=False)
delivered_date = models.DateTimeField(null=True, blank=True)
creation_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)
due_date = models.DateTimeField(null=True, blank=True)
created_by = models.ForeignKey(User, related_name='created_by_username', on_delete=models.CASCADE, null=True, blank=True)
project = models.ForeignKey(Project, null=True, blank=True, on_delete=models.CASCADE)
file_name = models.FileField(upload_to='projects_files', null=True, blank=True)
notes = models.TextField()
def __str__(self):
return str(self.task_name)
#login_required
def addtask(request):
form = taskForm()
if request.method == 'POST':
form = taskForm(request.POST)
if form.is_valid():
newform = form.save(commit=False)
newform.created_by = request.user
newform.save()
return HttpResponseRedirect(request.path_info)
else:
context = {'form':form}
return render(request, 'tasks/add_task.html', context)
Update
As well pointed out by Ahmed I. Elsayed there is some inconsistency in the title of the question, since the created_by field is actually a ForeignKey, not a ManyToManyField.
That being said, your issue is actually with the foreign key.
My suggestion is to first of all be sure that your form is actually valid. You can do that by printing something inside the if form.is_valid() block.
I'm building an app in which I have a model with several manytomany fields and some foreignkey fields, and I need a view that allows me to create all in one page and let me enter multiply times for the manytomanyfields, but I don't know how to deal with a lot of forms in the same page/view.
main model
class Client(models.Model):
company_name = models.CharField(max_length=255, blank=True, null=True)
payment_method = models.CharField(max_length=64, null=True, blank=True)
owner = models.CharField(max_length=100, blank=True, null=True)
operation = models.CharField(max_length=50, blank=True, null=True)
value = models.DecimalField(max_digits=8, decimal_places=2, null=True, blank=True)
currency = models.CharField(max_length=55, null=True, blank=True)
address = models.ManyToManyField(Address, blank=True)
contact = models.ManyToManyField(Contact, blank=True)
relationship = models.ManyToManyField(Relationship, blank=True)
attachment = models.ManyToManyField(Attachment, blank=True)
billing = models.ForeignKey(Billing, null=True, blank=True, on_delete=models.CASCADE)
delivery = models.ForeignKey(Delivery, null=True, blank=True, on_delete=models.CASCADE)
author = models.ForeignKey(User, on_delete=models.DO_NOTHING, blank=True, null=True)
note = models.TextField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.company_name
others models
class Address(models.Model):
address = models.CharField(max_length=256, null=True, blank=True)
number = models.PositiveIntegerField(null=True, blank=True)
district = models.CharField(max_length=32, null=True, blank=True)
city = models.CharField(max_length=32, null=True, blank=True)
country = models.CharField(choices=COUNTRY_CHOICES, max_length=64, null=True, blank=True)
def __str__(self):
return self.address
class Contact(models.Model):
name = models.CharField(max_length=256, blank=False, null=True)
sector = models.CharField(max_length=64, blank=True, null=True)
email = models.EmailField(max_length=256, blank=True, null=True)
phone = PhoneField(blank=True, null=True)
cellphone = PhoneField(blank=True, null=True)
role = models.CharField(max_length=155, blank=True, null=True)
nfe = models.BooleanField(verbose_name='NFE', default=False)
def __str__(self):
return self.name
class Relationship(models.Model):
company = models.CharField(max_length=256, blank=False, null=True)
agent = models.ForeignKey(Vendedor, on_delete=models.DO_NOTHING, blank=True, null=True)
type = models.CharField(choices=TYPE_CHOICES, max_length=32, blank=True, null=True)
contact = models.CharField(max_length=128, blank=True, null=True)
def __str__(self):
return self.company
class Attachment(models.Model):
file = models.FileField(upload_to='post/img/%Y/%m/%d', blank=True, null=True)
note = models.CharField(verbose_name='Observação', max_length=256, blank=True, null=True)
author = models.ForeignKey(User, on_delete=models.DO_NOTHING)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.note
class BankAccount(models.Model):
bank = models.CharField(max_length=64, blank=False, null=True)
type = models.CharField(max_length=32, blank=False, null=True)
payment_method = models.CharField(max_length=64, blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.bank
class Billing(models.Model):
name = models.CharField(max_length=256, blank=True, null=True)
address = models.ForeignKey(Address, null=True, blank=True, on_delete=models.CASCADE)
email = models.EmailField(max_length=256, blank=True, null=True)
phone = PhoneField(blank=True, null=True)
contact = models.CharField(max_length=256, blank=True, null=True)
def __str__(self):
return self.name
class Delivery(models.Model):
name = models.CharField(max_length=256, blank=True, null=True)
address = models.ForeignKey(Address, null=True, blank=True, on_delete=models.CASCADE)
email = models.EmailField(max_length=256, blank=True, null=True)
phone = PhoneField(blank=True, null=True)
contact = models.CharField(max_length=256, blank=True, null=True)
def __str__(self):
return self.name
does anyone know what is the best way to deal with this?
Make a python file forms.py and make model forms for all the models you have. Then in views make a function with all the forms. Then in the html file call the different forms together using jinja2. Here is a simple example:
models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class UserProfileInfo(models.Model):
user = models.OneToOneField(User, on_delete = models.CASCADE)
Your_website = models.URLField(blank = True)
profile_pic = models.ImageField(upload_to="profile_pics", blank = True)
def __str__(self):
return self.user.username
This model is connected to the django user model by OneToOneField.
Then in forms.py I have created all the model forms for the models.
from django import forms
from django.contrib.auth.models import User
from basic.models import UserProfileInfo
from django.core import validators
class userform(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput())
re_enter_password = forms.CharField(widget=forms.PasswordInput())
class Meta():
model = User
fields = ("username", "email", "password")
class UserProfileInfoForm(forms.ModelForm):
class Meta():
model = UserProfileInfo
fields = ('Your_website', "profile_pic")
Then make the view with both the model forms.
def register(request):
registered = False
if request.method == 'POST':
user_form = forms.userform(data = request.POST)
profile_form = forms.UserProfileInfoForm(data = request.POST)
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
user.set_password(user.password)
user.save()
profile = profile_form.save(commit=False)
profile.user = user
if 'profile_pic' in request.FILES:
profile.profile_pic = request.FILES['profile_pic']
profile.save()
registered = True
else:
print(user_form.errors, profile_form.errors)
else:
user_form = forms.userform()
profile_form = forms.UserProfileInfoForm()
In the html file call both the forms like this:
<div class="container">
<form method="post" enctype="multipart/form-data" class="all_forms">
{% csrf_token %}
{{ user_form.as_p }}
{{ profile_form.as_p }}
<input type="submit" class="btn btn-primary" value="Register Now!">
</form>
</div>
This way you don't have to create multiple forms for the models, the user will be asked only one form where all the models will get their separate response from one form.
Attached below is the models.py I used for my project,
I've attached the photos of the issue as well.
Issue 1
For some reason, all the contents of the tables are not being displayed.
In the first table, first content goes missing.
In the Second table, First and the second contents go missing
Issue 2
The Inline function doesn't work. I tried going through the documentation and several youtube videos to find out how to handle the situation but it didn't help much.
Issue 3
For the bug table, When I select the project name, How do I ensure that only the people I added to that project table are allowed to be selected?
Issue 4
Is there a way to extract the email ID from the Users page and and when I choose the name of the user in the Project page, the email Id will be automatically filled it?
Same way, In the issues page, when I choose the user, the email Id will be auto entered.
MODELS.py
from django.db import models
# Create your models here.
from django.contrib.auth.models import User
from django.db import models
from django.core.mail import EmailMessage
from django.contrib import admin
# Create your models here.
class Project(models.Model):
STATUS_CHOICE = (
('Project Manager', 'Project Manager'),
('Technician', 'Technician'),
('Tester', 'Tester')
)
STATUS_CHOICE_1 = (
('Work Assigned', 'Work Assigned'),
('Work in Progress', 'Work in Progress'),
('Testing', 'Testing'),
('Completed', 'Completed')
)
Project_Name = models.CharField(max_length=100)
Project_Description = models.CharField(max_length=100)
Admin_Name = models.ForeignKey(User, on_delete=models.CASCADE, related_name='User.Admin_Name_users+')
Admin_Mail_ID = models.EmailField(max_length=50)
Project_Manager_1 = models.ForeignKey(User, on_delete=models.CASCADE, related_name='User.Project_Manager_1_users+')
Project_Manager_1_Mail_ID = models.EmailField(max_length=50)
Project_Manager_2 = models.ForeignKey(User, on_delete=models.CASCADE, related_name='User.Project_Manager_2_users+', blank=True, null=True)
Project_Manager_2_Mail_ID = models.EmailField(max_length=50, blank=True, null=True)
Technician_1 = models.ForeignKey(User, on_delete=models.CASCADE, related_name='User.Technician_1_users+')
Technician_1_Mail_ID = models.EmailField(max_length=50)
Technician_2 = models.ForeignKey(User, on_delete=models.CASCADE, related_name='User.Technician_2_users+', blank=True, null=True)
Technician_2_Mail_ID = models.EmailField(max_length=50, blank=True, null=True)
Technician_3 = models.ForeignKey(User, on_delete=models.CASCADE, related_name='User.Technician_3_users+', blank=True, null=True)
Technician_3_Mail_ID = models.EmailField(max_length=50, blank=True, null=True)
Tester_1 = models.ForeignKey(User, on_delete=models.CASCADE, related_name='User.Tester_1_users+')
Tester_1_Mail_ID = models.EmailField(max_length=50, default='Example#gmail.com')
Additional_User_1 = models.ForeignKey(User, on_delete=models.CASCADE, related_name='User.Ad_1_users+', blank=True, null=True)
Additional_User_1_Type = models.CharField(max_length=18, choices=STATUS_CHOICE, blank=True, null=True)
Additional_User_1_Mail_ID = models.EmailField(max_length=50, blank=True, null=True)
Additional_User_2 = models.ForeignKey(User, on_delete=models.CASCADE, related_name='User.Ad_1_users+', blank=True, null=True)
Additional_User_2_Type = models.CharField(max_length=18, choices=STATUS_CHOICE, blank=True, null=True)
Additional_User_2_Mail_ID = models.EmailField(max_length=50, blank=True, null=True)
Additional_User_3 = models.ForeignKey(User, on_delete=models.CASCADE, related_name='User.Ad_1_users+', blank=True, null=True)
Additional_User_3_Type = models.CharField(max_length=18, choices=STATUS_CHOICE, blank=True, null=True)
Additional_User_3_Mail_ID = models.EmailField(max_length=50, blank=True, null=True)
Status_of_the_project = models.CharField(max_length=18, choices=STATUS_CHOICE_1)
Created = models.DateTimeField(auto_now_add=True, null=True, blank=True)
Finish_Date = models.DateTimeField(null=True, blank=True)
Supporting_Documents = models.FileField(null=True, blank=True)
class FlatPageAdmin(admin.ModelAdmin):
fieldsets = (
(None, {
'fields': ('Project_Name','Project_Description','Admin_Name','Admin_Mail_ID','Project_Manager_1','Project_Manager_1_Mail_ID',
'Technician_1','Technician_1_Mail_ID','Tester_1','Tester_1_Mail_ID','Status_of_the_project','Created','Finish_Date','Supporting_Documents',
)
}),
('Add More Users', {
'classes': ('collapse',),
'fields': ('Project_Manager_2','Project_Manager_2_Mail_ID','Technician_2','Technician_2_Mail_ID',
'Technician_3','Technician_3_Mail_ID','Additional_User_1','Additional_User_1_Type',
'Additional_User_1_Mail_ID','Additional_User_2','Additional_User_2_Type','Additional_User_2_Mail_ID',
'Additional_User_3','Additional_User_3_Type','Additional_User_3_Mail_ID'),
}),
)
def __str__(self):
return self.Project_Name
class Meta:
verbose_name_plural = "List Of Projects"
class Bug(models.Model):
STATUS_CHOICE = (
('Unassigned', 'Unassigned'),
('Assigned', 'Assigned'),
('Testing', 'Testing'),
('Tested', 'tested'),
('Fixed', 'Fixed')
)
STATUS_CHOICE_1 = (
('Bug', 'Bug'),
('Issue', 'Issue'),
('Enhancement', 'Enhancement'),
('Not an issue or bug', 'Not an issue or bug'),
('Fixed', 'Fixed')
)
Project = models.ForeignKey(Project, on_delete=models.CASCADE)
Issue_Title = models.CharField(max_length=50, blank=True, null=True)
Situation_Type = models.CharField(max_length=25, choices=STATUS_CHOICE_1)
Basic_Description = models.CharField(max_length=100)
Detailed_Description = models.TextField(default='The Description, here.')
Status = models.CharField(max_length=18, choices=STATUS_CHOICE)
Assigned_to = models.ForeignKey(User, on_delete=models.CASCADE)
Assigned_to_Mail_ID = models.EmailField(max_length=50, blank=True, null=True)
Admin_Mail_ID = models.EmailField(max_length=50, blank=True, null=True)
Reported_by = models.CharField(max_length=50, blank=True, null=True)
Reporters_Mail_ID = models.EmailField(max_length=50, blank=True, null=True)
Reported_Date = models.DateTimeField(null=True, blank=True)
Created = models.DateTimeField(auto_now_add=True, null=True, blank=True)
Updated = models.DateTimeField(auto_now=True, null=True, blank=True)
Deadline_Date = models.DateTimeField(null=True, blank=True)
Supporting_Documents_By_Reporter = models.FileField(null=True, blank=True)
Project_Managers_Comment = models.TextField(default='The Description, here.')
Supporting_Documents_by_Project_Manager = models.FileField(null=True, blank=True)
Technicians_Comment = models.TextField(default='The Description, here.')
Supporting_Documents_by_Technician = models.FileField(null=True, blank=True)
Testers_Comment = models.TextField(default='The Description, here.')
Supporting_Documents_by_Tester = models.FileField(null=True, blank=True)
def __str__(self):
return '{} ({}) [{}]'.format(self.Project, self.Situation_Type, self.Status, self.Issue_Title)
def save(self, force_insert=False, force_update=False, using=None,
update_fields=None):
if self.id:
user=self.Assigned_to
self.Assigned_to_Mail_ID=user.email
send_mail(self.Admin_Mail_ID, ass=self.Assigned_to_Mail_ID)
super(Bug, self).save()
class Meta:
verbose_name_plural = "Projects Tasks/Issues"
def send_mail(admin,ass):
email=EmailMessage('Changes made to Task','Changes have been made to one of your Task reports and we hereby request you to have a look at it at the earliest.', to=[admin,ass])
email.send()
I've also attached the admin.py file as well.
Admin.py
from django.contrib import admin
from .models import Bug, Project
from django.contrib.admin.models import LogEntry
admin.site.register(LogEntry)
# Register your models here.
class BugDisplay(admin.ModelAdmin):
list_display = ('Project', 'Status', 'Basic_Description', 'Assigned_to', 'Created', 'Updated', 'Issue_Title')
list_filter = ('Status', 'Assigned_to', 'Project')
search_fields = ('Reporters_Mail_ID', 'Reported_by', 'Basic_Description',)
admin.site.register(Bug, BugDisplay)
# Register your models here.
#admin.register(Project)
class ProjectDisplay(admin.ModelAdmin):
list_display = ('Project_Name','Admin_Name', 'Project_Manager_1', 'Status_of_the_project')
list_filter = ('Admin_Name', 'Status_of_the_project')
search_fields = ('Project_Name', 'Project_Description', 'Admin_Name', 'Admin_Mail_ID', 'Project_Manager_1 '
'Project_Manager_1_Mail_ID', 'Project_Manager_2 ', 'Project_Manager_2_Mail_ID',
'Technician_1',
'Technician_1_Mail_ID', 'Technician_2', 'Technician_2_Mail_ID', 'Technician_3',
'Technician_3_Mail_ID', 'Tester_1', 'Tester_1_Mail_ID', 'Additional_User_1', 'Additional_User_1_Type',
'Additional_User_1_Mail_ID', 'Additional_User_2', 'Additional_User_2_Type', 'Additional_User_2_Mail_ID',
'Additional_User_3', 'Additional_User_3_Type', 'Additional_User_3_Mail_ID', 'Status_of_the_project', 'Created',
'Finish_Date', 'Supporting_Documents')
As per your question please provide your admin inline class (referring to issue 2) and please elaborate for issue 1 and provide more details.
Regarding issue 2 you can override appropriate field using this. You can provide your custom queryset(by applying your filter conditions) to the foreign key field.
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == 'your_field_name':
kwargs["queryset"] = your_custom_queryset_based_on_conditions
return super().formfield_for_foreignkey(db_field, request, **kwargs)
For issue 4, you want email Id to be auto filled based on user selection. This can be achieved using custom javascript.
Another way could be that, you do not show email field and while saving the record from project/issue admin you automatically fetch email id from users table and assign it to the object and save it.
You may have a look at this, to customize while saving the object from admin panel.
def save_model(self, request, obj, form, change):
email = your_method_to_find_email_for_user(obj.user_field)
obj.email_field = email
super().save_model(request,obj,form,change)