Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django / mySQL - AttributeError: 'ForeignKey' object has no attribute 'IntegerField'

I have a django project which works fine on windows. and I am trying to move it to a ubuntu. There are some problem exist when I run python manage.py runserver 8000

File "/home/zhaojf1/Web-Interaction-APP/fileUpload_app/models.py", line 151, in Machine number_pins = models.IntegerField(blank=True, null=True)

AttributeError: 'ForeignKey' object has no attribute 'IntegerField'

Also this column in the Machine table is not a foreign key item.

Code in views.py:

140 class Machine(models.Model):
141     model = models.ForeignKey('Model', db_column='model', blank=True, null=True)
142     sn = models.CharField(max_length=50, blank=True)
143     mine_lon = models.CharField(max_length=50, blank=True)
144     mine_lat = models.CharField(max_length=50, blank=True)
145     location = models.CharField(max_length=50, blank=True)
146     total_hours = models.IntegerField(blank=True, null=True)
147     travel_hours = models.IntegerField(blank=True, null=True)
148     machine_id = models.IntegerField(primary_key=True)
149     models = models.ForeignKey('Model', db_column='models', blank=True, null=True)
150     # photo_set_num = models.IntegerField(blank=True, null=True)
151     number_pins = models.IntegerField(blank=True, null=True)
152     class Meta:
153         managed = False
154         db_table = 'machine'

I have a mysql database and I generate the models.py directly from mysql using

$ python manage.py inspectdb > models.py

like image 407
KKlalala Avatar asked Sep 10 '26 15:09

KKlalala


1 Answers

Your field named models shadows the models imported from Django. You can either rename the field:

other_name_for_models = models.ForeignKey('Model', db_column='models', blank=True, null=True)

or import the module with a different name

from django.db import models as django_models

class Machine(django_models.Model):
    models = django_models.ForeignKey('Model', db_column='model', blank=True, null=True)
like image 165
Seb D. Avatar answered Sep 13 '26 06:09

Seb D.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!