How to write out an object from a relationship in django

Advertisements I don’t know how to describe this exactly in database jargon. I would like to output an object that is in a relationship with another like this. order__orderitem.product.title models.py class Order(models.Model): status_types = [ ("Ordered", "Ordered"), ("Sent", "Sent"), ("Delivered", "Delivered"), ("Extended", "Extended"), ("Returned", "Returned"), ] user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) order_date = models.DateTimeField() return_date =… Read More How to write out an object from a relationship in django

How to apply select_related to the objects of a m2m relationship in Django?

Advertisements Lets say there is a structure like this: class Aaaaa(models.Model): b = models.ManyToManyField(‘Bbbbb’) class Bbbbb(models.Model): c = models.ForeignKey(‘Ccccc’) class Ccccc(models.Model): x = models.CharField(max_lenght="3") Now I’m in the DetailView of Aaaaa. I do prefetch_related(‘b’). But how can I let Django know to get all the "Ccccc" as well? >Solution : You can work with a… Read More How to apply select_related to the objects of a m2m relationship in Django?

i get this error OperationalError at /admin/accounts/student/

Advertisements hey guys i work withe django framework i had the error OperationalError i have to class with same fields class Student(models.Model): user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE) id = models.AutoField(primary_key=True) First_Name = models.CharField(‘First Name’, max_length=30, null=True, blank=True) Last_Name = models.CharField(‘Last Name’, max_length=30, null=True, blank=True) ID_Number = models.CharField(‘Id Number’, max_length=30, null=True, blank=True) Phone = PhoneNumberField(‘Phone’,null=True)… Read More i get this error OperationalError at /admin/accounts/student/

Losing microseconds when inserting date to database

Advertisements I have this code snippet in a view: time_from = datetime.strptime(req.POST[‘time_from’], "%Y-%m-%d %H:%M:%S.%f") with connections["mssql_database"].cursor() as cursor: sql_statement = "EXEC SaveActivity @TimeFrom=’%s’" % (time_from) print(sql_statement) cursor.execute(sql_statement) It prints this SQL statement: EXEC SaveActivity @TimeFrom=’2021-12-01 08:34:54′ Microseconds are missing. They are zeros, but I need them in a database. How can I correct this? >Solution… Read More Losing microseconds when inserting date to database

Django: I am unable to get Django to autocreate an autofield

Advertisements I am calling BORROWER.objects.create(ssn=Ssn, bname=Name, address=Address, phone=Phone) from views.py to create an entry in my sqlite database. This is my models.py file with the relevant function. class BORROWER(models.Model): card_id = models.AutoField(primary_key=True, max_length=7) ssn = models.CharField(max_length=11) bname = models.CharField(max_length=71) address = models.CharField(max_length=79) phone = models.CharField(max_length=15) def __str__(self): return str(self.card_id) The database entry is successfully created.… Read More Django: I am unable to get Django to autocreate an autofield