Sascha Wick Sascha Wick - 5 months ago 37
Ajax Question

Get the ChildObject from the BaseObject

So i want to get the Secondobject

It's One to One from First.

class Firstobject(models.Model):
def save(self,*args,**kwargs):

class Votes(models.Model):
first = models.OneToOneField(First)
def save(self,*args,**kwargs):
self.value2 = self.value - self.value2
def __str__(self):
return "%s name" % self.first

def function(request, pk):
pk = request.GET.get('pk')
obj = models.First.objects.get(pk=pk)
votes = obj.Second
votes.value = votes.value + 1
data = {'val1':votes.value, 'val2':votes.value2}
return JsonResponse(data)


data-url="{% url 'first:link' %}"


$('button').click(function() {
var pk = $(this).attr("data-pk");
var urla = $(this).attr("data-url"); //
url : urla,
data : {'pk' : pk},
success : function(data) {
$('#ID1' + pk).text(data.val1)
$('#ID2' + pk).text(data.val2)}

So my Error is:

AttributeError at /link/5/ 'First' object has no attribute 'Second'

I know whats the error is, but i cant figure out how to fix it.

The Error is in I cant just
. So can you tell me what method i should use?

Answer Source

Do add related_name

first = models.OneToOneField(First, related_name="second")

Then you can do

obj = models.First.objects.get(pk=pk)
votes = obj.second
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download