Setting an existing field to use ForeignKey can't access the object

12 views
Skip to first unread message

Patrick Carra

unread,
Sep 11, 2020, 10:53:25 AM9/11/20
to Django users
hello I am attempting to relate two existing tables 

class Circuitinfotable(models.Model):
    id1 = models.AutoField(primary_key=True, null=False, unique=True)
    pid = models.CharField(max_length=255, blank=True, null=True)
    circuitid = models.CharField(max_length=255, blank=True, null=True)
    bandwidth = models.CharField(max_length=255, blank=True, null=True, choices=bandwidth_list)
    region = models.CharField(max_length=255, blank=True, null=True, choices=region_list)
    bw = models.IntegerField(blank=True, null=True)
    pathname = models.CharField(max_length=255, blank=True, null=True)
    handoffalocaddress = models.CharField(max_length=255, blank=True, null=True)
    handoffaloccity = models.CharField(max_length=255, blank=True, null=True)
    handoffalocst = models.CharField(max_length=255, blank=True, null=True)
    alocationaddress = models.CharField(max_length=255, blank=True, null=True)
    alocationcity = models.CharField(max_length=255, blank=True, null=True)

class Budgettable(models.Model):
    id = models.CharField(primary_key=True, max_length=255)
    circuitid = models.CharField(max_length=255, blank=True, null=True)
    pid = models.CharField(max_length=255, blank=True, null=True)
    monthnum = models.IntegerField(blank=True, null=True)
    yearnum = models.IntegerField(blank=True, null=True)
    budgetmrc = models.TextField(blank=True, null=True)  # This field type is a guess.
    actualmrc = models.TextField(blank=True, null=True)  # This field type is a guess.
    region = models.CharField(max_length=255, blank=True, null=True)
    circuitref = models.ForeignKey(Circuitinfotable, on_delete=models.CASCADE, to_field='id1')

    class Meta:
        managed = False

If open a shell and import Circuitinfotable and Budget and run the following code:
from finance.models import Circuitinfotable, Budget
c=Circuitinfotable.objects.get(id1=695)
c.budget
c.budget_set.all

I get an error:
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'Circuitinfotable' object has no attribute 'budget'

or 

Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'Circuitinfotable' object has no attribute 'budget_set'

I set up my budget file to have the circuitref populated with the id1 field of the Circuitinfotable.

In my code I can query these two objects and manually relate them myself in the view but I cannot get the django ORM to do this for me.  Any suggestions?

Ogunsanya Opeyemi

unread,
Sep 11, 2020, 11:43:39 AM9/11/20
to django...@googlegroups.com
Your ciecuitinfotable model does not have budget and budget_set field
--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/350ce6b2-d63b-45ae-bdc6-f9b123b3d04cn%40googlegroups.com.


--
OGUNSANYA OPEYEMI

Patrick Carra

unread,
Sep 11, 2020, 12:59:56 PM9/11/20
to Django users
OGUNSANYA I'm trying to access the related objects in the budget table from the Circuitinfotable.  From what I can tell once they are related using the ForeignKey I should be able to do this with c.budget but it doesn't work for me.

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.


--
OGUNSANYA OPEYEMI

Patrick Carra

unread,
Sep 11, 2020, 1:15:01 PM9/11/20
to Django users
Okay I figured it out. First I was using the wrong table (an old table that I should have deleted out). Second I needed to set the db_column attribute in my ForeignKey options).  Works like a charm now.  Sorry but thanks for the help!
Reply all
Reply to author
Forward
0 new messages