Reputation: 399
I have created a foreign key field in the "ReviewComments" model and have included the command "order_with_respect_to". However, the drop-down box that appears in the webpage does not sort the service providers by alphabetical order. Is there something that the code is missing?
Models.py
class ServiceProvider(models.Model):
identification_number = models.AutoField(primary_key=True, )
license_number = models.CharField(max_length=10, null=True, blank=True, )
individual_name = models.CharField(max_length=60, )
corporate_name = models.CharField(max_length=120, )
reg_address = models.CharField(max_length=180, )
email_address = models.EmailField(max_length=254, null=True, blank=True, )
land_line = models.CharField(max_length=50, null=True, )
hand_phone_line = models.CharField(max_length=50, null=True, blank=True, )
service_type = models.CharField(max_length=20)
def __str__(self):
return "%s (%s)" % (self.individual_name, self.service_type)
class ReviewComments(models.Model):
comment_id = models.AutoField(primary_key=True)
title = models.CharField(max_length=120)
comment = models.CharField(max_length=250)
receipt_document = models.FileField(upload_to='receipt_document', default=None, )
service_provider = models.ForeignKey(ServiceProvider, on_delete=models.CASCADE)
user_id = models.CharField(max_length=10, null=True, blank=True, )
class Meta:
order_with_respect_to = 'service_provider'
def __str__(self):
return self.title + "\n" + self.comment
Forms.py
class ReviewForm(ModelForm):
class Meta:
model = ReviewComments
fields = ['title', 'comment', 'service_provider', 'receipt_document', ]
widgets = {
'title': Textarea(attrs={'cols': 80, 'rows': 1}),
'comment': Textarea(attrs={'cols': 80, 'rows': 10}),
}
Upvotes: 0
Views: 293
Reputation: 31404
order_with_respect_to
is not what you are looking for. That will order your ReviewComments
with respect to their corresponding ServiceProvider
- it does not alter the order of the ServiceProvider
.
If you want your service providers to be listed alphabetically then you need to set the default ordering
on the the ServiceProvider
model, specifying which field you want to order by:
class ServiceProvider(models.Model):
# ...
class Meta:
ordering = ['individual_name']
Upvotes: 3