0

I've been searching around for how to do this an di think i broke my table

I tried adding

dealership = models.ForeignKey('Employee', null=True)

To the field to the models.py, since I noticed thats where my other column fields were, and now the entire table is gone.

After some more research I saw that its supposed to be added to the migrations location and then run

$ python models.py migrations

Heres the model I want to add it to

## Gate access log - receives from RPi via API
##
class Eventlog(models.Model):
    ENTRY = 1
    EXIT = 2
    EVENT_CHOICES = (
        (ENTRY, 'Entry'),
        (EXIT, 'Exit'),
    )
    event = models.PositiveSmallIntegerField(choices=EVENT_CHOICES)
    employee = models.ForeignKey('Employee', null=True)
    timestamp = models.DateTimeField(auto_now_add=True)
    status = models.BooleanField(default=True, help_text="Whether the employee was granted access")

    def __unicode__(self):
        return self.timestamp

    def __str__(self):
        return self.timestamp

and as the comment suggests, it pulls the from a raspberry pi through an api

My question is how do I properly add the column, the db already has the information for the column data I can't imagine it's that difficult to simply pull that info and how do I get my table back? The table seems to have vanished after I added to the models.py manually and when I tried undoing it just never came back.

2
  • Does the table already have the dealership column and you want to add it on the model? Or is it a new column in an existing model? Commented Mar 13, 2018 at 21:56
  • Yes the column is already added and has its placement holder. I want to add it to an existing model called eventlog Commented Mar 13, 2018 at 22:14

2 Answers 2

1

You need to run two commands:

python manage.py makemigrations

then

python manage.py migrate
Sign up to request clarification or add additional context in comments.

6 Comments

is this with the added field i want in the migrations file or the models one?
the whole project
sorry if im being slow, just trying to make sure i get a full and complete understanding, whole project? meaning? both models and migrations? its already in my page template and in the postgresSQL DB as a cloumn
you need to go to the command line, and run makemigrations to create the migrations file. then ask Django to execute it with migrate. thats how you add, delete or modify models in Django
i got a traceback saying that module widget-tweaks was not found, i checked the settings i found this in my settings.py INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'core', 'api', 'widget_tweaks', ] but it looks like the widget-tweaks lives inside /ve/lib/python3.5/site-packages/widget_tweaks/ what am i doing wrong?
|
0

To add columns in postgresql db solution as below:

pip install django-extensions

and add this in the install app of settings.py

INSTALLED_APPS = [
    'django_extensions'
]

Now, run migrations for your installed apps

python manage.py makemigrations your_app_name
python manage.py migrtate your_app_name

Done! See Your Database...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.