Posts

Showing posts with the label django-models

How to optimize a sort based on “latest” related model

How to optimize a sort based on “latest” related model So say we have two models class Product(models.Model): """ A model representing a product in a website. Has new datapoints referencing this as a foreign key daily """ name = models.CharField(null=False, max_length=1024, default="To be Scraped") url = models.URLField(null=False, blank=False, max_length=10000) class DataPoint(models.Model): """ A model representing a datapoint in a Product's timeline. A new one is created for every product daily """ product = models.ForeignKey(Product, null=False) price = models.FloatField(null=False, default=0.0) inventory_left = models.BigIntegerField(null=False, default=0) inventory_sold = models.BigIntegerField(null=False, default=0) date_created = models.DateField(auto_now_add=True) def __unicode__(self): return "%s - %s" % (self.product.name, self.inventory_sold) T...

Django ModelForm: How to Add ForeignKey as Field in Form?

Image
Django ModelForm: How to Add ForeignKey as Field in Form? I have Three Models StudentInfo , StdSubject , Marks class StdSubject(StdCommon): REGULAR='R' OPTIONAL='O' SUBJECT_TYPE_CHOICE=( (REGULAR, 'REGULAR'), (OPTIONAL, 'OPTIONAL') ) subject_name = models.CharField('Subject Name', max_length=100) subject_type = models.CharField( 'Subject Type', max_length=1, default=REGULAR, choices=SUBJECT_TYPE_CHOICE) subject_full_marks = models.DecimalField( 'Full Marks', max_digits=5, decimal_places=2, default=100) subject_pass_marks = models.DecimalField( 'Pass Marks', max_digits=5, decimal_places=2) def __str__(self): return self.subject_name class StudentInfo(StdCommon): STD_CLASS=( ('6','Six'), ('7','Seven'), ('8','Eight'), ('9','Nine'), (...