# Get acess to fields in OnetoOneField models within forms

**URL:** <https://forum.djangoproject.com/t/get-acess-to-fields-in-onetoonefield-models-within-forms/13525>\
**Category:** Forms & APIs\
**Created:** [May 4, 2022, 8:11pm UTC](https://forum.djangoproject.com/t/get-acess-to-fields-in-onetoonefield-models-within-forms/13525 "2022-05-04T20:11:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kimiyori](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kimiyori/32/7742_2.png) [@Kimiyori](https://forum.djangoproject.com/u/Kimiyori)\
**Post date:** [May 4, 2022, 8:11pm UTC](https://forum.djangoproject.com/t/get-acess-to-fields-in-onetoonefield-models-within-forms/13525/1 "2022-05-04T20:11:28Z")

</div>

Hello, I have a problem and will be very happy if someone can help. I have a the following model

```auto
class Post(models.Model):
    ...
    title = models.CharField(max_length=500)
    related_to=models.ForeignKey(Related, related_name='post', 
              on_delete=models.CASCADE,blank=True,null=True)
    ...

```

where related\_to connected with

```auto
class Related(models.Model):
    book = models.OneToOneField(Book, related_name='related',null=True, blank=True,
                                 on_delete=models.CASCADE)
    novel = models.OneToOneField(Novel,related_name='related', null=True, blank=True,
                                 on_delete=models.CASCADE)
    
    def __str__ (self):
        if self.book:
            return self.book.title.original_name
        elif self.novel:
            return self.novel.title.original_name
        else:
             return 'None'

```

The Book and Novel models are associated with the Title model, which has an original\_name field. Also I have the following form to edit the fields I need

```auto
class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ['title', 'main_image', 'related_to']

```

My problem is that when i create a form like below, every time i make redundant sql queries because of my ` __str__ ` method and every time i get full list of fields of ` Book`/`Novel` models and also additional sql query to `Title` model,which is definitely redundant when I only need the name  
My problem is that I need to get from my Book/Rovel models only the id and original\_name field from the related Title model, but I don’t know how to access only those fields in my form.  
I thought about something like this ‘related\_to\_\_book\_\_title\_\_original\_name’ to avoid sql query to title model, but it doesnt work.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 4, 2022, 10:56pm UTC](https://forum.djangoproject.com/t/get-acess-to-fields-in-onetoonefield-models-within-forms/13525/2 "2022-05-04T22:56:14Z")

</div>

So the way to do this would be to not use the ` __str__ ` function to populate the select field. You can build the values for that select widget using a queryset, where the queryset uses the `select_related` clause (or `prefetch_related` depending upon the relationships) to get the values to be selected.
