Skip to content

How to get assigned nameserver of Zone model #117

Answered by peteeckel
kemeris2000 asked this question in Q&A
Discussion options

You must be logged in to vote

You are using values(), which unpacks the Zone object to a dict, and loses all related objects. What you need, however, is the object.

zone = Zone.objects.get(id=1)
zone.nameservers.all()
<RestrictedQuerySet [<NameServer: ns1.example.com>, <NameServer: ns2.example.com>]>

If you want to stick with the filter() expression that would be

zone = Zone.objects.filter(id=1).first()
zone.nameservers.all()
<RestrictedQuerySet [<NameServer: ns1.example.com>, <NameServer: ns2.example.com>]>

Note that get() and find() behave differently for non-existing objects: find() returns an empty QuerySet while get() raises a DoesNotExist exception you need to handle.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@kemeris2000
Comment options

Answer selected by peteeckel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants