What's new

Help Django ORM?

0R1-qt

Honorary Poster
Established
Joined
Nov 7, 2020
Posts
290
Reaction
55
Points
152
Hello Magandang gabi patulong po sa mga malulupet jan sa Django.
bali may for example may tatlo akong Car model.


class CarColor(models.Model):
colors = models.CharField(max_length=50)
def str(self) -> str:
return self.colors

class Car(models.Model):
car_name = models.CharField(max_length=50)
car_model = models.CharField(max_length=200)
car_description = models.TextField(max_length=200)
car_color = models.ForeignKey(CarColor, on_delete=models.CASCADE)
car_image = models.ImageField(null=True, blank=True, upload_to='media', default='imageholder.png')
date_manufactured = models.DateTimeField(auto_now_add=True)

def get_absolute_url(self):
""" After creating or updating a Car, this function will be triggered and redirection using reverse"""
return reverse("car_detail", kwargs={"pk": self.pk})

class Meta:
ordering = ['date_manufactured']

def str(self):
return f'{self.car_name}'
 
Hello!

It looks like you have three Car models and you are using a ForeignKey to associate one CarColor to each Car. This is a good approach to organize your data.

In your Car model, you have defined a str method which returns the name of the car. This is a good practice as it provides a human-readable representation of your model instance.

If you have any specific questions or issues with your Django project, please let me know and I'll do my best to assist you.
 
Hello!

It looks like you have three Car models and you are using a ForeignKey to associate one CarColor to each Car. This is a good approach to organize your data.

In your Car model, you have defined a str method which returns the name of the car. This is a good practice as it provides a human-readable representation of your model instance.

If you have any specific questions or issues with your Django project, please let me know and I'll do my best to assist you.
Thank you for the comment
 
Back
Top