I have a simple django app that is using only the admin. This is the model as is now in the server:
from django.db import models
class School(models.Model):
school = models.CharField(max_length=200)
def __unicode__(self):
return self.school
class Lawyer(models.Model):
first = models.CharField(max_length=20)
initial = models.CharField(blank=True, max_length=2)
last = models.CharField(max_length=20)
year_graduated = models.IntegerField('Year graduated')
school = models.CharField(max_length=200)
school = models.ForeignKey(School)
class Meta:
ordering = ('last',)
def __unicode__(self):
return self.first
I want to add two new fields, so that the new models.py
looks like this:
from django.db import models
class School(models.Model):
school = models.CharField(max_length=300)
def __unicode__(self):
return self.school
class Lawyer(models.Model):
firm_url = models.URLField('Bio', max_length=200)
firm_name = models.CharField('Firm', max_length=100)
first = models.CharField('First Name', max_length=50)
last = models.CharField('Last Name', max_length=50)
year_graduated = models.IntegerField('Year graduated')
school = models.CharField(max_length=300)
school = models.ForeignKey(School)
class Meta:
ordering = ('?',)
def __unicode__(self):
return self.first
Otherwise everything else is the same. Can I just upload this new models.py
to the server and expect the application to work the same as before? I also need to clear what is in the database. I am using sqlite3
.
I tried to deploy a new app but unfortunately the hosting co is refusing to help. Since I don't need two apps I thought about replacing models.py
. This new app works in the django dev server as expected. I would appreciate advice since I will take care of the other outstanding questions after I make this app running. Thanks!
Edit
Thanks for all the answers. Since I want the database clear I will try jcd
's solution. So, I will replace the old fields in the models.py
first = models.CharField(max_length=20)
initial = models.CharField(blank=True, max_length=2)
last = models.CharField(max_length=20)
year_graduated = models.IntegerField('Year graduated')
school = models.CharField(max_length=200)
school = models.ForeignKey(School)
with the new fields:
firm_url = models.URLField('Bio', max_length=200)
firm_name = models.CharField('Firm', max_length=100)
first = models.CharField('First Name', max_length=50)
last = models.CharField('Last Name', max_length=50)
year_graduated = models.IntegerField('Year graduated')
school = models.CharField(max_length=300)
school = models.ForeignKey(School)
to have
from django.db import models
class School(models.Model):
school = models.CharField(max_length=200)
def __unicode__(self):
return self.school
class Lawyer(models.Model):
firm_url = models.URLField('Bio', max_length=200)
firm_name = models.CharField('Firm', max_length=100)
first = models.CharField('First Name', max_length=50)
last = models.CharField('Last Name', max_length=50)
year_graduated = models.IntegerField('Year graduated')
school = models.CharField(max_length=300)
school = models.ForeignKey(School)
class Meta:
ordering = ('last',)
def __unicode__(self):
return self.first
and upload this to the server. Then I will run manage.py syncdb
.
And when I open the page I will see the admin
as it was before except with new fields.
I assume it is no problem to update the admin.py
later to add the new fields:
class LawyerAdmin(admin.ModelAdmin):
fieldsets = [
('Name', {'fields': ['first', 'last', 'firm_name', 'firm_url']}),
('School', {'fields': ['school', 'year_graduated']}),
]
list_display = ('first', 'last', 'school', 'year_graduated', 'firm_name', 'firm_url')
list_filter = ['year_graduated']
#search_fields = ['last', 'first']
search_fields = ['school__school']
search_fields = ['school__lawyer__last']
Thanks again!