.py file is not compiled on django - django

I'm using django 1.6.1. I got one file name "payment.py" on models folder and it's not compiled to .pyc file. Even if I changed the file name to "payment_room.py", it still won't be compiled
The other model files have been compiled already and worked pretty well.
Because of that, django won't create the table on my database if I run syncdb command. I run validate command using manage.py, but it didn't return any error.
N.B.
It's still on debug & development mode. The web hasn't been deployed to apache server yet.
from django.db import models
from ghb_manager.models.reservation import Reservation
class Payment (models.Model):
class Meta:
app_label = 'ghb_manager'
PAYMENT_METHOD_CHOICES = (
('CA', 'Cash'),
('CR', 'Credit'),
('TR', 'Transfer')
)
payment_id = models.CharField(max_length=14, primary_key=True)
reservation_id = models.ForeignKey(Reservation)
ammount_tendered = models.DecimalField(max_digits=19, decimal_places=2)
payment_method = models.CharField(max_length=20, choices=PAYMENT_METHOD_CHOICES)
payment_date = models.DateField()
def __unicode__(self):
return self.payment_id

Please check your init.py in your models directory. You have to import all the models you create in models directory, like:
# __init__.py
from payment import Payment
Django looks into app.models for app's models.

Related

Django error: multiple default values for specified column "id"

I'm working with a project with docker-compose, where I have a postgre container.
When I run:
docker-compose -f dev.yml run django python manage.py migrate
I get the error:
django.db.utils.ProgrammingError: multiple default values specified for column "id" of table "scrapy_scrapy"
That is happening before I made some changes to my models.py file. But now the file is correct and should be working. This is the content of the models.py file:
from django.db import models
import django
# Create your models here.
class Scrapy(models.Model):
user = models.CharField(max_length=50,blank=True)
password = models.CharField(max_length=50,blank=True)
projecte = models.CharField(max_length=100)
estat_last_check = models.CharField(max_length=700, default="", blank=True)
date = models.DateTimeField(default=django.utils.timezone.now, blank=True)
app_label = ''
def __str__(self): # __unicode__ on Python 2
return self.projecte + " - " + self.user
class Meta:
app_label = 'scrapy'
As you can see, no id filed is defined anymore, so, why is complaining about that field?
I've done my research and tried some possible solutions, but no luck. I've already tried deleting the full Docker container and creating it again, or trying to delete the database.
Any ideas?

unique_together does not work in Django shell

I have below model:
class Property(models.Model):
job = models.ForeignKey(Job, on_delete=models.CASCADE)
app = models.ForeignKey(App, on_delete=models.CASCADE)
name = models.CharField(max_length=120)
value = models.CharField(max_length=350, blank=True)
description = models.TextField(blank=True)
pub_date = models.DateTimeField('date_published', default=timezone.now)
class Meta:
verbose_name_plural = "properties"
unique_together = (('value', 'name'),)
def __str__(self):
return self.name
When I try to create a Property object in admin page (I'm using Django Suit) with name/value which are already exist I get the exception: "Property with this Value and Name already exists." So it works perfect.
But in manage.py shell:
>>>from myapp.models import App, Property, Job
>>>from django.shortcuts import get_object_or_404
>>>app = get_object_or_404(App, app_name='BLABLA')
>>>job = get_object_or_404(Job, job_name='BLABLA2')
>>> Property.objects.create(job=job, app=app, name='1', value='1')
<Property: 1>
>>> Property.objects.create(job=job, app=app, name='1', value='1')
<Property: 1>
In this case I do not get any exceptions and objects are added in database.
I tried makemigrations, migrate and migrate --run-syncdb.
Django 1.9.12, sqlite3
The unique constraints are enforced at database level. You're not getting any error probably because SQLite doesn't support this type of constraint. You cannot add constraint to existing table in SQLite. If you're in early development stage, drop the table and recreate it with updated constraints. Then it should work fine in shell.
Check SQLite alter table docs for allowed updates on an existing table.
The admin form throws error because it checks uniqueness by itself without relying on database constraints.

Select field for all models within a Django app

I am pretty new to Django.
I wanted to create a form for some user information. Depending on type of user informations, the fields should change... For example the users private address needs the fields name, street, zip and city. But if he wants something send to the company, there might be more fields like department or company name.
I want to implement something like this and create for each kind of input an extra model compact in a separate app.
Is there a way to get a select field with a list of all available models in this app.
Edit
Since I have some further problems, I add an example here
file: experiment/models.py
from django.db import models
from django.apps import apps
class BasicExperiment(models.Model):
date_created = models.DateTimeField(editable=False)
date_modified = models.DateTimeField(blank=True)
label_app = apps.get_app('labels')
label_types = apps.get_models(label_app)
file: labels/models.py
from django.db import models
class SILAC(models.Model):
lys0 = models.BooleanField('Lys-0', default=True)
lys4 = models.BooleanField('Lys-4', default=None)
lys8 = models.BooleanField('Lys-8', default=None)
arg0 = models.BooleanField('Arg-0', default=True)
arg6 = models.BooleanField('Arg-6', default=None)
arg10 = models.BooleanField('Arg-10', default=None)
class Meta:
verbose_name = 'SILAC Labeling'
In the shell it works as expected:
>>> from django.apps import apps
>>> app = apps.get_app('labels')
>>> for model in apps.get_models(app):
... model._meta.verbose_name
...
'SILAC Labeling'
Within my models.py I get the following error:
...
File "/Users/madejung/Documents/django_dev/cfproteomics/experiments/models.py", line 5, in <module>
class BasicExperiment(models.Model):
File "/Users/madejung/Documents/django_dev/cfproteomics/experiments/models.py", line 10, in BasicExperiment
label_app = apps.get_app('labels')
File "/Library/Python/2.7/site-packages/django/apps/registry.py", line 370, in get_app
"App '%s' doesn't have a models module." % app_label)
django.core.exceptions.ImproperlyConfigured: App 'labels' doesn't have a models module.
You could try this:
from django.db.models import get_app, get_models
app = get_app('my_application_name')
for model in get_models(app):
# do something with the model
Here there is more information Django get list of models in application

Django South DatabaseError: App inside app - Table doesn't exist

After being inspired by django-oscar, I tried to put a cmsplugin inside its main app. However, after doing a schemamigration --initial I can't use the plugin in Django CMS.
DatabaseError: (1146, "Table 'devel_test.cmsplugin_galleries_gallerycontainer_galleries' doesn't exist")
App Tree
gallery
apps
cmsplugin_galleries
migrations
cms_plugins.py
init.py
models.py
init.py
migrations
static
templates
admin.py
init.py
models.py
views.py
Gallery models
class Gallery(models.Model):
title = models.CharField(_(u'title'), max_length=200)
description = models.TextField(_(u'description'), blank=True)
image_folder = FilerFolderField(verbose_name=_(u'image folder'))
is_video = models.BooleanField(_(u'is video content'), default=False)
snippet = models.TextField(_(u'video snippet'))
class Meta:
verbose_name = _(u'Gallery')
verbose_name_plural = _(u'Galleries')
class GalleryImage(models.Model):
gallery = models.ForeignKey(Gallery, verbose_name=_(u'gallery'))
title = models.CharField(_(u'title'), max_length=200, blank=True)
src = FilerImageField(verbose_name=_(u'image'))
Cmsplugin_galleries models
class GalleryContainer(CMSPlugin):
title = models.CharField(_(u'title'), max_length=200)
galleries = models.ManyToManyField(Gallery, verbose_name=_(u'galleries'))
def __unicode__(self):
return u'%s' % self.title
Since I can properly run it using syncdb --all, what did I do wrong?
Notes:
Using syncdb creates a table with the name cmsplugin_galleries_gallerycontainer_galleries but using south, the table name is cmsplugin_gallerycontainer_galleries
Thanks
Since there was no other alternative I followed #daigorocub tip and replaced all the shorten_name instances of cmsplugin_gallerycontainer_galleries inside the migration file
m2m_table_name = db.shorten_name(u'cmsplugin_gallerycontainer_galleries')
to
m2m_table_name = db.shorten_name(u'cmsplugin_galleries_gallerycontainer_galleries')
This issue was fixed in the development branch of django cms 3.0 (https://github.com/divio/django-cms/issues/2291#ref-pullrequest-26529456) but since my version is older I couldn't update to the latest branch
Thank you guys for your help!

modelform field name can't be localized when in production

I have a model:
class Server(models.Model):
serverId = models.IntegerField(verbose_name=_("serverId"))
name = models.CharField(max_length=200, verbose_name=_("server_name"))
ip = models.CharField(max_length=200, verbose_name=_("ip"))
cport = models.IntegerField(default=5000, verbose_name=_("cport"))
aport = models.IntegerField(default=1000, verbose_name=_("aport"))
hport = models.IntegerField(default=2000, verbose_name=_("hport"))
version = models.CharField(max_length=100, verbose_name=_("version"))
serverGroup = models.ForeignKey(Group, null=True, blank=True,
verbose_name=_('server_group'))
class Meta:
db_table = u'server'
def __unicode__(self):
return self.name
and the modelform:
class ServerForm(ModelForm):
class Meta:
model = Server
from within this app directory I did
$ mkdir locale
$ django-admin.py makemessages -l zh_CN
then I provided the translation in locale/zh_CN/LC_MESSAGES/django.po
then I did
$ django-admin.py compilemessages
then I ran the development server:
$ python manage.py runserver
and went to look at the url http://127.0.0.1:8000 in firefox and the translation displayed. So I thought I did right and I deployed the project on the same machine using nginx + fastcgi with nothing changed in the whole project. Then I go to the url http://127.0.0.1, and then the the modelform shows English there. It didn't localize to Chinese.
I've googled much and read many docs from docs.djangoproject.com and still don't know how to solve the problem. So I ask here.
I only set LANGUAGE_CODE = 'zh_CN' in my settings.py and leave everything on deafult. My django version is 1.2.4
Any of your comments are appreciated.
Make sure you are using lazy_translation. Are you importing ugettext_lazy or ugettext?
from django.utils.translation import ugettext_lazy as _
http://docs.djangoproject.com/en/dev/topics/i18n/internationalization/#lazy-translation
For information, django takes the first argument as the verbose name of a field.
So you can also write your models in a shorter way, like this :
version = models.CharField(_("version"), max_length=100)
serverGroup = models.ForeignKey(_('server_group'), Group, null=True, blank=True)
On version 1.4 it works only (for me) with ugettext not with ugettext_lazy