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!

On Friday, September 11, 2020 at 11:59:56 AM UTC-5 Patrick Carra wrote:

> 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.
>
> On Friday, September 11, 2020 at 10:43:39 AM UTC-5 Ogunsanya Opeyemi wrote:
>
>> Your ciecuitinfotable model does not have budget and budget_set field
>>
>> On Friday, September 11, 2020, Patrick Carra <pcar...@gmail.com> wrote:
>>
>>> 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?
>>>
>>> -- 
>>> 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/350ce6b2-d63b-45ae-bdc6-f9b123b3d04cn%40googlegroups.com
>>>  
>>> <https://groups.google.com/d/msgid/django-users/350ce6b2-d63b-45ae-bdc6-f9b123b3d04cn%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>
>>
>> -- 
>> OGUNSANYA OPEYEMI
>>
>>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/0c0d7b78-c04c-45b0-af41-a29b76715021n%40googlegroups.com.

Reply via email to