I'm Trying to save an Item with the user's officeid but it's throwing me an error
ValueError: Cannot assign "<CustomUser: admin#gmail.com>": "ClearanceItem.office" must be a "Office" instance.
My customuser has id of 1
while the email is admin#gmail.com
lastly the officeid = 'OSA'
this is my models.py
class CustomUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(_('email address'), unique=True)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
date_joined = models.DateTimeField(default=timezone.now)
userid = models.CharField(null=True, max_length=9)
officeid = models.ForeignKey('Office', models.DO_NOTHING, db_column='officeid', blank=True, null=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = CustomUserManager()
class ClearanceItem(models.Model):
cl_itemid = models.CharField(primary_key=True, max_length=20, default=get_default_id)
studid = models.CharField(max_length=9, blank=True, null=True)
office = models.ForeignKey('Office', models.DO_NOTHING, blank=True, null=True)
sem = models.CharField(max_length=1, blank=True, null=True)
sy = models.CharField(max_length=9, blank=True, null=True)
remarks = models.TextField(blank=True, null=True)
resolution = models.TextField(blank=True, null=True)
resolve = models.BooleanField(default='False', blank=True, null=True)
resolve_date = models.DateField(blank=True, null=True)
resolve_by = models.CharField(max_length=8, blank=True, null=True)
recorded_by = models.CharField(max_length=8, blank=True, null=True)
record_date = models.DateField(auto_now_add = True, blank=True, null=True)
class Meta:
managed = False
db_table = 'clearance_item'
class Office(models.Model):
office_id = models.CharField(primary_key=True, max_length=50)
office_name = models.CharField(max_length=200)
office_head = models.CharField(max_length=8, blank=True, null=True)
designation = models.TextField(blank=True, null=True)
office_parent = models.CharField(max_length=50, blank=True, null=True)
deptlogo = models.TextField(blank=True, null=True)
class Meta:
managed = False
db_table = 'office'
this is my views.py
class APIClerkView(generics.ListCreateAPIView):
permission_classes = [IsAuthenticated]
serializer_class = ClearanceItemSerialize
def perform_create(self, serializer):
serializer.save(office=CustomUser.objects.get(officeid=self.request.user.officeid),
recorded_by=self.request.user.userid)
Based on my previous encounter of this problem i think, it's not throwing an actual officeid but instead a string
How can I remedy that or Any alternative solution
My goal here is to avoid users type their officeid in a field along with other details.
{
"sem": "1",
"sy": "2022-2023",
"remarks": "TEST",
"resolution": "TEST",
"studid": "2012-5037"
}
you can do this way
class APIClerkView(generics.ListCreateAPIView):
permission_classes = [IsAuthenticated]
serializer_class = ClearanceItemSerializer
def perform_create(self, serializer):
try:
office=CustomUser.objects.get(officeid=self.request.user.officeid)
except CustomUser.DoesNotExist:
raise ValidationError({'error':'User does not exist'}
# you can now do whatever you want with the instance
Related
Excuse me devs, i wanna ask about how to count on drf serializer, i need codes that can serialized fields plant from table A and it relations with another table B with count of them "plants_active"
Here's my code:
# Models
class TablePlants(models.Model):
plant_id = models.CharField(primary_key=True, max_length=20, unique=True)
gateway = models.ForeignKey(
TableGatewayDevice, models.DO_NOTHING, blank=True, null=True)
name = models.CharField(max_length=150, blank=True, null=True)
date = models.DateField(blank=True, null=True)
contact_person = models.CharField(max_length=70, blank=True, null=True)
contact_email = models.CharField(max_length=50, blank=True, null=True)
contact_phone = models.CharField(max_length=30, blank=True, null=True)
plant_status = models.CharField(max_length=20, blank=True, null=True)
weather_status_code = models.ForeignKey(
TableAuxWeather, models.DO_NOTHING, db_column='weather_status_code', blank=True, null=True)
timezone = models.CharField(max_length=200, blank=True, null=True)
image = models.FileField(
upload_to='plants/', validators=[file_size, validate_file_extension], null=True, blank=True)
class Meta:
db_table = 'table_plants'
def __str__(self):
return 'TablePlants[id: {id}, name: {name}]'.format(
id=self.id, name=self.name)
class PVOwner(models.Model):
pv_owner_id = models.AutoField(primary_key=True)
company = models.ForeignKey(TableCompany, on_delete=CASCADE,
blank=True, null=True, related_name="pv_owner_company")
class Meta:
db_table = 'table_pv_owner'
class TableSitePlant(models.Model):
pv_owner = models.ForeignKey(
PVOwner, on_delete=CASCADE, blank=True, null=True, related_name="pv_site_owner_plant")
site_owner = models.ForeignKey(
SiteOwner, on_delete=CASCADE, blank=True, null=True, related_name="site_owner_plant")
plant = models.ForeignKey(TablePlants, on_delete=CASCADE,
blank=True, null=True, related_name="site_plant")
class Meta:
db_table = 'table_site_plant'
# Serializer
class MainMenuSerializer(serializers.ModelSerializer):
plants_active = serializers.IntegerField(source="plant")
class Meta:
model = TableSitePlant
fields = ['plants_active']
# Views
#permission_classes([AllowAny])
class OverviewPlantsActiveView(generics.RetrieveAPIView):
queryset = TableSitePlant.objects.all().filter(plant__plant_status='offline')
serializer_class = OverviewPlantsActiveSerializer
lookup_field = 'pv_owner'
What i expecting is i can count how many plants that have status online
You can use the get method to return response as you desired.
#permission_classes([AllowAny])
class OverviewPlantsActiveView(generics.RetrieveAPIView):
queryset = TableSitePlant.objects.all().filter()
serializer_class = OverviewPlantsActiveSerializer
lookup_field = 'pv_owner'
def get(self, request):
queryset = self.get_queryset().filter(plant__plant_status='online')
return Response({
"active_plants": queryset.count(),
})
I'm working on a project where a form is filled out. And in that form you have the is_active fields. If the user selects True, it means the account is active and if it is False, it means it is no longer active, and the user can no longer see it. Through the filters I'm trying to present only the forms with the is_active True, but I'm not able to.
Follow one of my attempts in my views:
class BookingViewSet(viewsets.ModelViewSet):
serializer_class = BookingSerializer
#queryset = Booking.objects.all()
#queryset = Booking.objects.filter(is_active="True")
#filter_backends = (filters.DjangoFilterBackend,)
#filterset_class = BookingFilter
#filterset_fields = ['is_active']
def get_queryset(self):
queryset = Booking.objects.all()
username = self.request.query_params.get('bookings')
if username is not None:
queryset = queryset.filter(is_active__username=username)
return queryset
and here are my models
class Booking(models.Model):
booking_id = models.AutoField(primary_key=True)
account = models.ForeignKey(Account, models.DO_NOTHING)
tenant = models.ForeignKey(Tenant, models.DO_NOTHING)
full_name = models.CharField(max_length=128)
email = models.CharField(max_length=256)
phone = models.CharField(max_length=256)
postal_code = models.CharField(max_length=64, null=True, blank=True)
from_city = models.CharField(max_length=256, null=True, blank=True)
to_city = models.CharField(max_length=256, null=True, blank=True)
travel_date = models.DateField()
travel_period = models.CharField(max_length=256, null=True, blank=True)
adults_travelers_count = models.SmallIntegerField()
children_travelers_count = models.SmallIntegerField()
senior_travelers_count = models.SmallIntegerField()
booking_request_description = models.TextField(blank=True, null=True)
booking_status_cd = models.CharField(max_length=10, null=True, blank=True)
locator_code = models.CharField(max_length=32, null=True, blank=True)
total_booking_price_atm = models.DecimalField(max_digits=11, decimal_places=2)
total_booking_cost_atm = models.DecimalField(max_digits=11, decimal_places=2)
payment_type_cd = models.CharField(max_length=10, null=True, blank=True)
payment_status_cd = models.CharField(max_length=10, null=True, blank=True)
payment_datetime = models.DateTimeField(blank=True, null=True)
discount_percent = models.DecimalField(max_digits=3, decimal_places=1)
discount_amount = models.DecimalField(max_digits=12, decimal_places=2)
payment_amount = models.DecimalField(max_digits=12, decimal_places=2)
voucher_file_path = models.TextField(blank=True, null=True)
receipt_file_path = models.TextField(blank=True, null=True)
invoice_file_path = models.TextField(blank=True, null=True)
modified_ts = models.DateTimeField()
modified_by = models.CharField(max_length=31)
modified_op = models.CharField(max_length=1)
created_by = models.CharField(max_length=31)
created_ts = models.DateTimeField()
is_active = models.BooleanField()
class Meta:
managed = True
db_table = 'booking'
unique_together = (("booking_id", "account", "tenant"),)
Essentially, you're correct that the filter Booking.objects.filter(is_active=True) will give you all the active bookings. If you also want to filter by username, you need to have another filter (can comma-separate in the same filter function) to filter by that. It's unclear where username lives in your models, but assuming it's on the account model:
Booking.objects.filter(is_active=True, account__username=username)
def get_queryset(self):
queryset = []
user = self.request.user
if user.is_authenticated:
bookings = Bookings.objects.all()
for i in bookings:
if i.is_active
queryset.append(i)
return queryset
This should do it!!
So I am new to Django and I have been reading a lot of documentation to figure this out, I have a table called "Logs" that has logs of different positions (has FK of table "Position"), each position belongs to a department (has FK to table "Department") Check the image below :1
What I want to do is create a view just like this one :
2
and whenever you click on a department, it extends all the positions in it with their respective logs like this :
3
The Screenshots I have attached are my work in main app (or if you would like to call it front end), I wanted to replicate the same process in the Django Admin page, I keep seeing that I should use inlines but I can't seem to make it work, can someone help or put me in the right direction please ? much appreciated.
Here is what I have in my models.py :
from django.db import models
from django.contrib.auth.models import User
from PIL import Image
class Site(models.Model):
site = models.CharField(max_length=200, blank=True, null=True)
totalHC = models.IntegerField(blank=True, null=True)
def __str__(self):
return self.site
class Department(models.Model):
department = models.CharField(max_length=200, blank=True)
def __str__(self):
return self.department
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
site = models.ForeignKey(Site, on_delete=models.CASCADE, null=True, default=Site(id="1").site)
department = models.ForeignKey(
"Department", on_delete=models.CASCADE, null=True)
image = models.ImageField(default='default.jpg', upload_to='profile_pics')
bio = models.CharField(max_length=2000, blank=True)
skills = models.CharField(max_length=2000, blank=True)
aoi = models.CharField(max_length=2000, blank=True)
github = models.CharField(max_length=200, blank=True)
linkedin = models.CharField(max_length=200, blank=True)
def __str__(self):
return f'{self.user.username} Profile'
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
img = Image.open(self.image.path)
if img.height > 300 or img.width > 300:
output_size = (300, 300)
img.thumbnail(output_size)
img.save(self.image.path)
class Grade(models.Model):
user = models.OneToOneField(Profile, on_delete=models.CASCADE)
ut1 = models.CharField(max_length=200, blank=True)
ut2 = models.CharField(max_length=200, blank=True)
ut3 = models.CharField(max_length=200, blank=True)
ut1p = models.ImageField(upload_to='plots', blank=True)
ut2p = models.ImageField(upload_to='plots', blank=True)
ut3p = models.ImageField(upload_to='plots', blank=True)
ut1pb = models.ImageField(upload_to='plots', blank=True)
ut2pb = models.ImageField(upload_to='plots', blank=True)
ut3pb = models.ImageField(upload_to='plots', blank=True)
ut12 = models.ImageField(upload_to='plots', blank=True)
ut13 = models.ImageField(upload_to='plots', blank=True)
ut23 = models.ImageField(upload_to='plots', blank=True)
class Section(models.Model):
class Meta:
verbose_name = 'Department'
verbose_name_plural = 'Departments'
section = models.CharField(max_length=200, blank=True)
def __str__(self):
return self.section
class Question(models.Model):
class Meta:
verbose_name = 'Position'
verbose_name_plural = 'Positions'
section = models.ForeignKey(
"Section", on_delete=models.CASCADE, null=True, blank=True)
question_field = models.CharField(max_length=2000, blank=True, null=True)
def __str__(self):
return self.question_field
class Answer(models.Model):
class Meta:
verbose_name = 'Log'
verbose_name_plural = 'Logs'
question = models.ForeignKey(Question, on_delete=models.CASCADE)
user = models.ForeignKey(Profile, on_delete=models.CASCADE)
answer_field = models.CharField(max_length=2000, blank=True, null=True)
def __str__(self):
return f"{self.user} answered {self.answer_field}"
class Position1(models.Model):
class Meta:
verbose_name = 'Position'
verbose_name_plural = 'Positions'
department = models.ForeignKey(
"Department", on_delete=models.CASCADE, null=True, blank=True)
position = models.CharField(max_length=200, blank=True)
jobID = models.CharField(max_length=200, blank=True)
class HCtype(models.TextChoices):
Staff = 'Staff', ('Staff')
IDL = 'IDL', ('IDL')
DL = 'DL', ('DL')
hctype = models.CharField(
max_length=5,
choices=HCtype.choices,
)
def __str__(self):
return self.position
class Log(models.Model):
position = models.ForeignKey(Position1, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
site = models.ForeignKey(Site, on_delete=models.CASCADE)
INN = models.IntegerField(blank=True, null=True)
OUT = models.IntegerField(blank=True, null=True)
date = models.CharField(max_length=200, blank=True)
internal = models.IntegerField(default=0, null=True)
class SiteHasPosition(models.Model):
date = models.CharField(max_length=200, blank=True)
site = models.ForeignKey(Site, on_delete=models.CASCADE)
position = models.ForeignKey(Position1, on_delete=models.CASCADE)
value = models.IntegerField(blank=True, null=True)
standard = models.IntegerField(blank=True, null=True)
turn_over = models.IntegerField(blank=True, null=True)
class SiteHasDepartment(models.Model):
date = models.CharField(max_length=200, blank=True)
site = models.ForeignKey(Site, on_delete=models.CASCADE)
department = models.ForeignKey(Department, on_delete=models.CASCADE)
value = models.IntegerField(blank=True, null=True)
class SiteKPIs(models.Model):
site = models.ForeignKey(Site, on_delete=models.CASCADE)
date = models.CharField(max_length=200, blank=True)
staff = models.IntegerField(blank=True, null=True)
dl = models.IntegerField(blank=True, null=True)
idl = models.IntegerField(blank=True, null=True)
total_hc = models.IntegerField(blank=True, null=True)
total_in = models.IntegerField(blank=True, null=True)
total_out = models.IntegerField(blank=True, null=True)
staff_rate = models.IntegerField(blank=True, null=True)
dl_rate = models.IntegerField(blank=True, null=True)
idl_rate = models.IntegerField(blank=True, null=True)
Here is how I registred them in admin.py :
admin.site.register(Profile)
admin.site.register(Log)
admin.site.register(Position1)
admin.site.register(Department)
admin.site.register(Site)
admin.site.register(SiteHasDepartment)
admin.site.register(SiteHasPosition)
I would like to have a page in admin.py where I can select a site and for that specific site display :
all the departments(when you press a dpt all the positions will expand) for each position the standardHC, attributes from the Log table (that match that position,and that site) and attributes from SiteHasPosition( that match the site and that position)
I hope I made it clearer
Default User Model:
class User(AbstractBaseUser, PermissionsMixin):
avatar = models.ImageField(upload_to='user/avatar', null=True, blank=True)
date_joined = models.DateField(auto_now_add=True)
username = models.EmailField(unique=True, null=False, blank=False)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_employer = models.BooleanField(default=False)
is_employee = models.BooleanField(default=False)
object = managers.UserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = []
class Meta:
verbose_name = 'User'
verbose_name_plural = 'Users'
Employer model:
class Employer(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, primary_key=True)
name = models.CharField(max_length=256, blank=False, null=False)
address = models.CharField(max_length=256, blank=False, null=False)
fax = models.DecimalField(max_digits=11, decimal_places=2, blank=True, null=True)
email = models.EmailField(unique=True, blank=False, null=False)
economic_code = models.DecimalField(max_digits=20, decimal_places=2, blank=True, null=True)
national_id = models.DecimalField(max_digits=20, decimal_places=2, blank=True, null=True)
Employee model:
class Employee(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, primary_key=True)
employer = models.ForeignKey(Employer, null=False, blank=False, on_delete=models.CASCADE)
first_name = models.CharField(max_length=50, null=False, blank=False)
last_name = models.CharField(max_length=100, null=False, blank=False)
national_id = models.PositiveIntegerField(null=False, blank=False)
date_of_birth = models.DateField(blank=False, null=False)
post = models.CharField(max_length=100, null=True, blank=True)
mobile = models.DecimalField(max_digits=11, decimal_places=2, null=False, blank=False)
personnel_code = models.PositiveIntegerField(null=True, blank=True)
eligible_leave = models.FloatField(default=0, blank=False, null=False)
sick_leave_per_month = models.FloatField(default=0, null=False, blank=False)
rfid_card_code = models.CharField(max_length=256, blank=False, null=False)
I want the employer to be authenticated by email and the employee by national code (username filed). How?
you need a custom authentication backend. let's name it backends.py
from django.contrib.auth.backends import ModelBackend
class EmployeeAuthentication(ModelBackend):
"""
Employee Backend
Allows a user to sign in using national_id and password.
"""
def authenticate(self, request, **kwargs):
national_id = kwargs.get('username')
password = kwargs.get('password')
try:
employee = Employee.objects.get(national_id=national_id)
if employee.user.check_password(password) is True:
return employee.user
except Employee.DoesNotExist:
pass
class EmployerAuthentication(ModelBackend):
"""
Employer Backend
Allows a user to sign in using email and password.
"""
def authenticate(self, request, **kwargs):
email = kwargs.get('username')
password = kwargs.get('password')
try:
employer = Employer.objects.get(email=email)
if employer.user.check_password(password) is True:
return employer.user
except Employer.DoesNotExist:
pass
and in settings.py
AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
'app.backends.EmployerAuthentication',
'app.backends.EmployeeAuthentication',
]
I have a template which displays a master detail form represented by a formset object. That part is working fine. I have a second detail model which is read-only that I would like to display on the same template as the master-detail form.
My view:
def order_edit(request, pk):
order = get_object_or_404(Orders, pk=pk)
if request.method == "POST":
form = OrderForm(request.POST, instance=order)
if form.is_valid():
order = form.save(commit=False)
lineitem_formset = LineFormSet(request.POST, instance=order)
if lineitem_formset.is_valid():
order.save()
lineitem_formset.save()
#order.date_ordered = timezone.now()
#order.save()
return redirect('order_list')
else:
form = OrderForm(instance=order)
#lineitem_formset = LineFormSet(instance=Orders()) This was wrong. It was calling a new instance of Orders which was blank
lineitem_formset = LineFormSet(instance=order)
modification_formset = ModificationFormSet(instance=order)
return render(request, "orders/order_edit.html", {"form": form, "lineitem_formset": lineitem_formset,"modification_formset": modification_formset })
I have read a number of posts on this topic but cannot seem to make sense of rendering the third model on my template. Here are my models.
class LineitemInfo(models.Model):
order = models.ForeignKey('Orders')
line_item_num = models.CharField(max_length=20)
item_description = models.CharField(max_length=1020, blank=True, null=True)
quantity = models.FloatField(blank=True, null=True)
unit = models.CharField(max_length=20, blank=True, null=True)
unit_price = models.FloatField(blank=True, null=True)
line_account_code = models.CharField(max_length=260, blank=True, null=True)
options = models.CharField(max_length=30, blank=True, null=True)
option_num = models.CharField(max_length=8, blank=True, null=True)
class Meta:
unique_together = (('order', 'line_item_num'),)
class Orders(models.Model):
pr_num = models.CharField(max_length=80, blank=True, null=True)
po_num = models.CharField(max_length=56, blank=True, null=True)
task_order_num = models.CharField(max_length=40, blank=True, null=True)
credit_card_id = models.CharField(max_length=40, blank=True, null=True)
date_ordered = models.DateField(blank=True, null=True)
vendor_name = models.CharField(max_length=200, blank=True, null=True)
order_description = models.CharField(max_length=400, blank=True, null=True)
predicted_order_total = models.DecimalField(max_digits=20, decimal_places=2, blank=True, null=True)
requestor = models.CharField(max_length=120, blank=True, null=True)
class Modifications(models.Model):
order = models.ForeignKey('Orders', editable=False)
mod_num = models.CharField(max_length=40, blank=True, null=True, editable=False)
mod_date = models.DateField(blank=True, null=True, editable=False)
mod_amount = models.IntegerField(blank=True, null=True, editable=False)
mod_description = models.CharField(max_length=400, blank=True, null=True, editable=False)
mod_performenddate = models.DateField(blank=True, null=True, editable=False)
optionyear = models.IntegerField(blank=True, null=True, editable=False)
class Meta:
unique_together = (('order', 'mod_num'),)
It is the third model that I am trying to render on the template. The first two are rendered as part of a formset. TIA.