How to access region_name value in my template.its giving null value

On Monday, November 7, 2022 at 4:25:24 AM UTC+5:30 Ryan Nowakowski wrote:

> On Fri, Nov 04, 2022 at 01:50:48AM -0700, Kala Rani wrote:
> > models.py
> > class Regions(models.Model): 
> > region_name = models.CharField(max_length=255) 
> > 
> > class Locations(models.Model): region = models.ForeignKey(Regions, 
> > on_delete=models.CASCADE,blank=True,null=True) name = 
> > models.CharField(max_length=255) 
>
> Here are some suggestions to make your code better. I would make your
> model class names singular, for example Region instead of Regions.
> That's Django best-practice. I'd also rename "region_name" to just
> "name" since it's a part of the Region model, you already know it's the
> name of the region.
>
> > How to do this query in django?
> > select locations.name,regions.region_name from locations inner join 
> regions 
> > on locations.region_id = regions.id order by locations.name; 
>
> With your current model naming:
>
> Locations.objects.order_by('name').values('name', 'region__region_name')
>
> With my suggestion changes above, it would be:
>
> Location.objects.order_by('name').values('name', 'region__name')
>
> > I want to show in select tag like country-city
>
> I'm not sure what you mean here.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/a6090492-cadd-4808-a068-d22ee0d63e64n%40googlegroups.com.

Reply via email to