How to get recent values in this db? - django

I use sqlite.
class Member(models.Model):
member_id = models.AutoField(primary_key=True)
is_update = models.IntegerField(default=0)
member_name = models.CharField(max_length=50)
member_group = models.IntegerField(default=0)
room_name = models.CharField(max_length=20)
bed_name = models.CharField(max_length=20)
gender = models.IntegerField(default=0)
birth_date = models.DateField()
phone_number = models.CharField(max_length=11)
protector = models.CharField(default='protector',max_length=50)
def __str__(self):
return str(self.member_id)
def initIsUpdate(self):
self.is_update = 0
return 0
class Inpatient(models.Model):
member_id = models.ForeignKey(Member, on_delete=models.CASCADE, db_column = 'member_id', related_name='member')
inpatient_status = models.IntegerField(default=0)
is_in_room = models.IntegerField(default=0)
is_on_bed = models.IntegerField(default=0)
heart_rate = models.IntegerField(default=0)
breath_rate = models.IntegerField(default=0)
update_time = models.DateTimeField(auto_now_add=True, blank=True)
protector_name = models.CharField(max_length=50)
def __str__(self):
return str(self.member_id)
And Members are 4 people..
Each Members have same values in Inpatient DB.
It gets new queryset for every 1 sec.
And I want get 4 Member's most recent Inpatient DB.
How I get that DB??

class Inpatient(models.Model):
member_id = models.ForeignKey(Member, on_delete=models.CASCADE, db_column = 'member_id', related_name='member')
inpatient_status = models.IntegerField(default=0)
is_in_room = models.IntegerField(default=0)
is_on_bed = models.IntegerField(default=0)
heart_rate = models.IntegerField(default=0)
breath_rate = models.IntegerField(default=0)
update_time = models.DateTimeField(auto_now_add=True, blank=True)
protector_name = models.CharField(max_length=50)
class Meta:
get_latest_by = "update_time"
def __str__(self):
return str(self.member_id)
Now when you filter the members in views.py it will provide recent values..

Related

Multiple try except in a serializer Django

I have a Warehouse model like the following:
class ShelfBin(models.Model):
bin_id = models.IntegerField(default=0)
bin_name = models.CharField(max_length=50, default=0)
class UnitShelf(models.Model):
shelf_id = models.IntegerField(default=0)
shelf_name = models.CharField(max_length=50, default=0)
bin = models.ManyToManyField(ShelfBin, blank=True)
class AisleUnit(models.Model):
unit_id = models.IntegerField(default=0)
unit_name = models.CharField(max_length=50, default=0)
shelf = models.ManyToManyField(UnitShelf, blank=True)
class ZoneAisle(models.Model):
aisle_id = models.IntegerField(default=0)
aisle_name = models.CharField(max_length=50, default=0)
unit = models.ManyToManyField(AisleUnit, blank=True)
class WarehouseZone(models.Model):
zone_id = models.IntegerField(default=0)
zone_name = models.CharField(max_length=50, default=0)
aisle = models.ManyToManyField(ZoneAisle, blank=True)
class Warehouse(models.Model):
owner = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=250, default=0)
address = models.CharField(max_length=500, default=0)
zones = models.ManyToManyField(WarehouseZone, blank=True)
for this I have created a serializer like the following:
class WarehouseSerializer(serializers.ModelSerializer):
zones = WarehouseZonesSerializer(many=True)
class Meta:
model = Warehouse
fields = "__all__"
def create(self, validated_data):
print("validated data warehouse", validated_data)
zone_objects = validated_data.pop('zones', None)
instance = Warehouse.objects.create(**validated_data)
for item in zone_objects:
aisle_objects = item.pop('aisle')
wz_obj = WarehouseZone.objects.create(**item)
for data in aisle_objects:
unit_objects = data.pop('unit')
za_obj = ZoneAisle.objects.create(**data)
for u_data in unit_objects:
shelf_objects = u_data.pop('shelf')
au_obj = AisleUnit.objects.create(**u_data)
for s_data in shelf_objects:
bin_objects = s_data.pop('bin')
us_obj = UnitShelf.objects.create(**s_data)
for b_data in bin_objects:
b_obj = ShelfBin.objects.create(**b_data)
us_obj.bin.add(b_obj)
au_obj.shelf.add(us_obj)
za_obj.unit.add(au_obj)
wz_obj.aisle.add(za_obj)
instance.zones.add(wz_obj)
return instance
Now the problem is that sometimes warehouse can have zone, aisle, units, etc(all 5 sub-levels) but sometimes it can only be 1,2 or 0 level deep
and in that cases it raises error like this :
aisle_objects = item.pop('aisle')
KeyError: 'aisle'
So do I have to use try and except at each level of the loop or is there a better way to handle these exceptions?

I want to assign a user to my card from the User table, but its not working for me

The error is:
django.db.utils.ProgrammingError: column
user_awaycardholder.assigned_user_id does not exist LINE 1:
...der"."id", "user_awaycardholder"."display_id_id", "user_away...
I am not able to create AwayCardHolder can you please tell me what is the above error ??? and How to fix that ?
class UserData(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
title = models.CharField(max_length=40, default="")
phone = models.CharField(max_length=15)
facebook_id = models.CharField(max_length=400)
youtube_id = models.CharField(max_length=400)
linkedin_id = models.CharField(max_length=400)
instagram_id = models.CharField(max_length=400)
twitter_id = models.CharField(max_length=400)
logo = models.ImageField(upload_to='logo')
profile_image = models.ImageField(upload_to='media/profile')
location = models.CharField(max_length=400)
website = models.CharField(max_length=100)
messanger = models.CharField(max_length=200)
parent_id = models.CharField(max_length=20, blank=True)
is_child = models.BooleanField(default=False)
phone_office = models.CharField(max_length=15, blank=True)
hits = models.IntegerField(max_length=10, default=0)
class Meta:
verbose_name_plural = "User Data"
def __str__(self):
name = self.user.first_name + ' ' + str(self.user.id)
return str(name)
class AwayCard(models.Model):
Away_id = models.UUIDField(default=uuid.uuid4, editable=True)
my_admin = models.ForeignKey(User, on_delete=models.CASCADE)
display_id = models.CharField(default='', max_length=30)
is_assigned = models.BooleanField(default=False)
def __str__(self):
name = str(self.display_id)
return str(name)
class AwayCardHolder(models.Model):
display_id = models.OneToOneField(AwayCard, on_delete=models.CASCADE)
assigned_user = models.ForeignKey(User, related_name="user_awaycardholder", on_delete=models.CASCADE)
def __str__(self):
name = self.display_id
return str(name)

How to take array of foreign keys in django model

I have a model product , price and custorder. I want a list of products while creating the order which is used as a foreign key in custorder. I am using postgres as database.
class Product(models.Model):
product = ArrayField(models.CharField(max_length=200), blank=True)
def __str__(self):
return str(self.product)
class Price(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
price = models.DecimalField(max_digits=50, decimal_places = 5, default=0)
def __str__(self):
return "%s" % self.price
class CustOrder(models.Model):
Customer_id = models.AutoField(primary_key=True)
CustomerName = models.CharField(max_length=200)
email = models.EmailField(max_length=70,blank=True, null= True, unique= True)
gender = models.CharField(max_length=6, choices=GENDER_CHOICES)
phone = PhoneField(null=False, blank=True, unique=True)
landmark = models.PointField()
#landmark = models.TextField(max_length=400, help_text="Enter the landmark", default='Enter landmark')
houseno = models.IntegerField(default=0)
#product_name = models.CharField(max_length=200, choices=PRODUCT_CHOICES,default='Boneless chicken')
product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True,related_name='pricetag')
quantity = models.IntegerField(default=0)
price = models.ForeignKey(Price, on_delete=models.SET_NULL, null=True,related_name='pricetag')
#price = models.DecimalField(max_digits=50, decimal_places=5, default=48.9)
pay_method = models.CharField(max_length=200,choices=PAYMENT_CHOICES, default='RAZOR PAY')
city = models.ForeignKey(City, on_delete=models.SET_NULL, null=True)
area = models.ForeignKey(Area, on_delete=models.SET_NULL, null=True)
Price.objects.aggregate(Sum('price'))
def __str__(self):
return self.CustomerName

django models getting queryset

how to get status_updation and status_date respect to the order_number
class customer_database(models.Model):
customer_id = models.CharField(max_length=20, unique=True)
customer_name = models.CharField(max_length=20)
customer_email = models.EmailField()
def __str__(self):
return self.customer_id
class order_database(models.Model):
order_number = models.CharField(max_length=10, unique=True, primary_key=True)
order_timestamp = models.DateTimeField()
order_consignment_number = models.CharField(max_length=20)
order_customer = models.ForeignKey(customer_database, on_delete=models.CASCADE)
def __str__(self):
return self.order_number
class track_database(models.Model):
order_status = models.ForeignKey(order_database, on_delete=models.CASCADE)
status_updation = models.CharField(max_length=30)
status_timestamp = models.DateTimeField()
def __str__(self):
return self.status_updation
Try
query_order_number = '1234'
tracker = track_database.objects.filter(order_status.order_number = query_order_number)
print(tracker.status_updation)
print(tracker.status_timestamp)
Let me know if this doesn't work
Or you could go via the order number:
query_order_number = '1234'
order = order_database.objects.filter(order_number = query_order_number)
print(order.track_database.status_updation)
print(order.track_database.status_timestamp)

Django - edit many-to-many inline

I have following model
class Day(models.Model):
date = models.DateField(auto_now=False, auto_now_add=False)
price = models.FloatField()
payment_method = models.CharField(max_length = 200, blank=True)
payment_date = models.CharField(max_length=200, blank=True)
room = models.ForeignKey(Room, null=True, blank=True, verbose_name='Номер', on_delete=models.CASCADE)
def __unicode__(self):
return str(self.date)
class Reservation(models.Model):
start = models.DateField(verbose_name='Заезд', auto_now=False, auto_now_add=False, blank=False)
end = models.DateField(verbose_name='Выезд', auto_now=False, auto_now_add=False, blank=False)
check_in_time = models.TimeField(verbose_name='Время заезда', blank=False)
check_out_time = models.TimeField(verbose_name='Время выезда', blank=False)
has_refund = models.BooleanField(verbose_name='Возвратная бронь', default=True)
payed = models.BooleanField(verbose_name='Оплачено', default=False)
reserved_days = models.ManyToManyField(Day, blank=False)
additional_services = models.ManyToManyField(AdditionalService)
guest_name = models.CharField(verbose_name='Имя гостя', max_length=200, blank=True)
reservation_number = models.CharField(verbose_name='Номер брони', max_length=200, blank=True)
What I want is to have ability to edit Day on Reservation page
I try the following as in the django docs
class ReservedDaysInline(admin.TabularInline):
model = Reservation
extra = 1
class ReservationAdmin(admin.ModelAdmin):
inlines = (ReservedDaysInline,)
class DayAdmin(admin.ModelAdmin):
inline = (ReservedDaysInline,)
admin.site.register(Reservation, ReservationAdmin)
admin.site.register(Day, DayAdmin)
But it doesnt work.
What am I doing wrong ?
Try this from doc
class ReservedDaysInline(admin.TabularInline):
model = Reservation.reserved_days.through
extra = 1
class ReservationAdmin(admin.ModelAdmin):
inlines = (ReservedDaysInline,)
exclude = ('reserved_days',)