merge fields from different table

62 views
Skip to first unread message

Kala Rani

unread,
Nov 4, 2022, 1:29:07 PM11/4/22
to Django users
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) 


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

I want to show in select tag like country-city

Ammar Mohammed

unread,
Nov 4, 2022, 2:37:15 PM11/4/22
to 'Rahul Chauhan' via Django users
Get your Locations object :
obj = Locations.objects.get(pk=1)
Then get the region:

obj.region.region_name 

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/29b954e2-646d-4b58-ba0d-381660ba2f33n%40googlegroups.com.

DUSHYANT SINGH

unread,
Nov 4, 2022, 9:34:30 PM11/4/22
to django...@googlegroups.com
hello i am not from the py community so please remove me from this section.
                                                   thanks

Kala Rani

unread,
Nov 5, 2022, 6:34:31 AM11/5/22
to Django users
<v-flex ml-2 md2>
<v-select :style="{padding:0,height:'30px'}"
v-model="filter.locations"
item-text="name"
item-value="id"
:items="locations"
label="Location Filter"
@change="paginate(status_id)"
multiple
required
:single-line = true
>

I want to show Location-region in v-select like mumbai-India.

How can I access region_name  from response and How can I show in v-select .
<script>
methods:{
fetchLocationsRegionList(){
this.$axios.$post(url,config).then(response => {
if(response){
this.locations =response

Ryan Nowakowski

unread,
Nov 6, 2022, 5:55:24 PM11/6/22
to Django users
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.

Kala Rani

unread,
Nov 6, 2022, 10:30:07 PM11/6/22
to Django users
How to access region_name value in my template.its giving null value
Reply all
Reply to author
Forward
0 new messages