Re: Dynamically adding a field to a model - via contribute_to_class

2017-09-13 Thread Melvyn Sopacua
If I may suggest a better approach:

- Make the code as ImageField does (see it's width_field and height_field)
- Then focus on writing the migration for this field

Unfortunately, I don't see a way for custom fields to provide hints to
the migrations framework. So a field that automatically creates
another field is hard to deal with.

Another different approach is to use a custom ForeignKey [1]. Then you
create only one field, but at the cost of a join.

[1] 
https://github.com/furious-luke/django-address/blob/master/address/models.py#L289

On Wed, Sep 13, 2017 at 2:05 PM, Michal Petrucha
 wrote:
> On Tue, Sep 12, 2017 at 12:34:26AM -0700, gernot.c...@gmail.com wrote:
>> Hi,
>>
>> I don't know if crossposting with stackoverflow is allowed here but
>> essentially my problem is explained
>> in 
>> https://stackoverflow.com/questions/46162104/django-dynamic-model-fields-and-migrations.
>> What I want to create is a custom ModelField of type DecimalField, that
>> dynamically adds another ModelField (CharField) to the source model. As far
>> as I can tell, this is nicely explained
>> in 
>> https://blog.elsdoerfer.name/2008/01/08/fuzzydates-or-one-django-model-field-multiple-database-columns/.
>>
>> Let's assume I start with a fresh project and app and add this code to
>> models.py:
>>
>> from django.db import models
>> from django.db.models import signals
>>
>>
>> _currency_field_name = lambda name: '{}_extension'.format(name)
>>
>>
>> class PriceField(models.DecimalField):
>>
>> def contribute_to_class(self, cls, name):
>> # add the extra currency field (CharField) to the class
>> if not cls._meta.abstract:
>> currency_field = models.CharField(
>> max_length=3,
>> editable=False,
>> null=True,
>> blank=True
>> )
>> cls.add_to_class(_currency_field_name(name), currency_field)
>> # add the original price field (DecimalField) to the class
>> super().contribute_to_class(cls, name)
>>
>> # TODO: set the descriptor
>> # setattr(cls, self.name, FooDescriptor(self))
>>
>>
>> class FooModel(models.Model):
>>
>> price = PriceField('agrh', decimal_places=3, max_digits=10, 
>> blank=True, null=True)
>>
>>
>> If I then call *./manage.py makemigrations* the following migration file
>> for the app is created:
>>
>> # Generated by Django 1.11.4 on 2017-09-11 18:02
>> from __future__ import unicode_literals
>>
>> from django.db import migrations, models
>> import testing.models
>>
>>
>> class Migration(migrations.Migration):
>>
>> initial = True
>>
>> dependencies = [
>> ]
>>
>> operations = [
>> migrations.CreateModel(
>> name='FooModel',
>> fields=[
>> ('id', models.AutoField(auto_created=True, primary_key=True, 
>> serialize=False, verbose_name='ID')),
>> ('price', testing.models.PriceField(blank=True, 
>> decimal_places=3, max_digits=10, null=True, verbose_name='agrh')),
>> ('price_extension', models.CharField(blank=True, 
>> editable=False, max_length=3, null=True)),
>> ],
>> ),
>> ]
>>
>>
>> All good, as far as I can tell. The problem comes up if I then call 
>> *./manage.py
>> migrate* which errors with the following exception:
>>
>> ./manage.py migrate testing
>> Operations to perform:
>>   Apply all migrations: testing
>> Running migrations:
>>   Applying testing.0001_initial...Traceback (most recent call last):
>>   File 
>> "/usr/local/var/pyenv/versions/stockmanagement-3.6.2/lib/python3.6/site-packages/django/db/backends/utils.py",
>>  line 63, in execute
>> return self.cursor.execute(sql)
>>   File 
>> "/usr/local/var/pyenv/versions/stockmanagement-3.6.2/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py",
>>  line 326, in execute
>> return Database.Cursor.execute(self, query)
>> sqlite3.OperationalError: duplicate column name: price_extension
>>
> [...]
>> As said, I have a fresh project with no exisiting DB and tables so far. Why
>> is it complaining that there allready exista a column named
>> "price_extension"?. The migration file only contains one field called
>> "price_extension"?
>
> I'm not quite certain about this, but I believe that when the
> migration runner reconstructs a version of your FooModel, it iterates
> over the list of fields, and adds each of them to the reconstructed
> model class one by one. So what happens is that your custom PriceField
> gets added, which in turn creates its price_extension field, and then
> next thing, the migration runner adds the price_extension field one
> more time. So you end up with two instances of the price_extension
> field on the same model, and when eventually the create_model schema
> operation is executed, it adds each of them as another column to the
> table, which is obviously wrong.
>
> As for what you could do to avoid 

Re: Dynamically adding a field to a model - via contribute_to_class

2017-09-13 Thread Michal Petrucha
On Tue, Sep 12, 2017 at 12:34:26AM -0700, gernot.c...@gmail.com wrote:
> Hi,
> 
> I don't know if crossposting with stackoverflow is allowed here but 
> essentially my problem is explained 
> in 
> https://stackoverflow.com/questions/46162104/django-dynamic-model-fields-and-migrations.
> What I want to create is a custom ModelField of type DecimalField, that 
> dynamically adds another ModelField (CharField) to the source model. As far 
> as I can tell, this is nicely explained 
> in 
> https://blog.elsdoerfer.name/2008/01/08/fuzzydates-or-one-django-model-field-multiple-database-columns/.
> 
> Let's assume I start with a fresh project and app and add this code to 
> models.py:
> 
> from django.db import models
> from django.db.models import signals
> 
> 
> _currency_field_name = lambda name: '{}_extension'.format(name)
> 
> 
> class PriceField(models.DecimalField):
> 
> def contribute_to_class(self, cls, name):
> # add the extra currency field (CharField) to the class
> if not cls._meta.abstract:
> currency_field = models.CharField(
> max_length=3, 
> editable=False,
> null=True, 
> blank=True
> )
> cls.add_to_class(_currency_field_name(name), currency_field)
> # add the original price field (DecimalField) to the class
> super().contribute_to_class(cls, name)
> 
> # TODO: set the descriptor
> # setattr(cls, self.name, FooDescriptor(self))
> 
> 
> class FooModel(models.Model):
> 
> price = PriceField('agrh', decimal_places=3, max_digits=10, 
> blank=True, null=True)
> 
> 
> If I then call *./manage.py makemigrations* the following migration file 
> for the app is created:
> 
> # Generated by Django 1.11.4 on 2017-09-11 18:02
> from __future__ import unicode_literals
> 
> from django.db import migrations, models
> import testing.models
> 
> 
> class Migration(migrations.Migration):
> 
> initial = True
> 
> dependencies = [
> ]
> 
> operations = [
> migrations.CreateModel(
> name='FooModel',
> fields=[
> ('id', models.AutoField(auto_created=True, primary_key=True, 
> serialize=False, verbose_name='ID')),
> ('price', testing.models.PriceField(blank=True, 
> decimal_places=3, max_digits=10, null=True, verbose_name='agrh')),
> ('price_extension', models.CharField(blank=True, 
> editable=False, max_length=3, null=True)),
> ],
> ),
> ]
> 
> 
> All good, as far as I can tell. The problem comes up if I then call 
> *./manage.py 
> migrate* which errors with the following exception:
> 
> ./manage.py migrate testing
> Operations to perform:
>   Apply all migrations: testing
> Running migrations:
>   Applying testing.0001_initial...Traceback (most recent call last):
>   File 
> "/usr/local/var/pyenv/versions/stockmanagement-3.6.2/lib/python3.6/site-packages/django/db/backends/utils.py",
>  line 63, in execute
> return self.cursor.execute(sql)
>   File 
> "/usr/local/var/pyenv/versions/stockmanagement-3.6.2/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py",
>  line 326, in execute
> return Database.Cursor.execute(self, query)
> sqlite3.OperationalError: duplicate column name: price_extension
> 
[...]
> As said, I have a fresh project with no exisiting DB and tables so far. Why 
> is it complaining that there allready exista a column named 
> "price_extension"?. The migration file only contains one field called 
> "price_extension"?

I'm not quite certain about this, but I believe that when the
migration runner reconstructs a version of your FooModel, it iterates
over the list of fields, and adds each of them to the reconstructed
model class one by one. So what happens is that your custom PriceField
gets added, which in turn creates its price_extension field, and then
next thing, the migration runner adds the price_extension field one
more time. So you end up with two instances of the price_extension
field on the same model, and when eventually the create_model schema
operation is executed, it adds each of them as another column to the
table, which is obviously wrong.

As for what you could do to avoid this situation – I'd suggest that
you add an extra argument to PriceField which tells the field that it
shouldn't create the additional extension field. Then, you implement
the deconstruct method on the field, and include this additional flag.
That way, auto-detected migrations will include the extra argument to
the field instance, and there shouldn't be any duplication of the
extension field on the resulting models reconstructed by migrations.

Good luck,

Michal

-- 
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 post to this group, send 

Dynamically adding a field to a model - via contribute_to_class

2017-09-12 Thread gernot . cseh
Hi,

I don't know if crossposting with stackoverflow is allowed here but 
essentially my problem is explained 
in 
https://stackoverflow.com/questions/46162104/django-dynamic-model-fields-and-migrations.
What I want to create is a custom ModelField of type DecimalField, that 
dynamically adds another ModelField (CharField) to the source model. As far 
as I can tell, this is nicely explained 
in 
https://blog.elsdoerfer.name/2008/01/08/fuzzydates-or-one-django-model-field-multiple-database-columns/.

Let's assume I start with a fresh project and app and add this code to 
models.py:

from django.db import models
from django.db.models import signals


_currency_field_name = lambda name: '{}_extension'.format(name)


class PriceField(models.DecimalField):

def contribute_to_class(self, cls, name):
# add the extra currency field (CharField) to the class
if not cls._meta.abstract:
currency_field = models.CharField(
max_length=3, 
editable=False,
null=True, 
blank=True
)
cls.add_to_class(_currency_field_name(name), currency_field)
# add the original price field (DecimalField) to the class
super().contribute_to_class(cls, name)

# TODO: set the descriptor
# setattr(cls, self.name, FooDescriptor(self))


class FooModel(models.Model):

price = PriceField('agrh', decimal_places=3, max_digits=10, blank=True, 
null=True)


If I then call *./manage.py makemigrations* the following migration file 
for the app is created:

# Generated by Django 1.11.4 on 2017-09-11 18:02
from __future__ import unicode_literals

from django.db import migrations, models
import testing.models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='FooModel',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, 
serialize=False, verbose_name='ID')),
('price', testing.models.PriceField(blank=True, 
decimal_places=3, max_digits=10, null=True, verbose_name='agrh')),
('price_extension', models.CharField(blank=True, 
editable=False, max_length=3, null=True)),
],
),
]


All good, as far as I can tell. The problem comes up if I then call 
*./manage.py 
migrate* which errors with the following exception:

./manage.py migrate testing
Operations to perform:
  Apply all migrations: testing
Running migrations:
  Applying testing.0001_initial...Traceback (most recent call last):
  File 
"/usr/local/var/pyenv/versions/stockmanagement-3.6.2/lib/python3.6/site-packages/django/db/backends/utils.py",
 line 63, in execute
return self.cursor.execute(sql)
  File 
"/usr/local/var/pyenv/versions/stockmanagement-3.6.2/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py",
 line 326, in execute
return Database.Cursor.execute(self, query)
sqlite3.OperationalError: duplicate column name: price_extension

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "./manage.py", line 22, in 
execute_from_command_line(sys.argv)
  File 
"/usr/local/var/pyenv/versions/stockmanagement-3.6.2/lib/python3.6/site-packages/django/core/management/__init__.py",
 line 363, in execute_from_command_line
utility.execute()
  File 
"/usr/local/var/pyenv/versions/stockmanagement-3.6.2/lib/python3.6/site-packages/django/core/management/__init__.py",
 line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
  File 
"/usr/local/var/pyenv/versions/stockmanagement-3.6.2/lib/python3.6/site-packages/django/core/management/base.py",
 line 283, in run_from_argv
self.execute(*args, **cmd_options)
  File 
"/usr/local/var/pyenv/versions/stockmanagement-3.6.2/lib/python3.6/site-packages/django/core/management/base.py",
 line 330, in execute
output = self.handle(*args, **options)
  File 
"/usr/local/var/pyenv/versions/stockmanagement-3.6.2/lib/python3.6/site-packages/django/core/management/commands/migrate.py",
 line 204, in handle
fake_initial=fake_initial,
  File 
"/usr/local/var/pyenv/versions/stockmanagement-3.6.2/lib/python3.6/site-packages/django/db/migrations/executor.py",
 line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, 
fake_initial=fake_initial)
  File 
"/usr/local/var/pyenv/versions/stockmanagement-3.6.2/lib/python3.6/site-packages/django/db/migrations/executor.py",
 line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, 
fake_initial=fake_initial)
  File 
"/usr/local/var/pyenv/versions/stockmanagement-3.6.2/lib/python3.6/site-packages/django/db/migrations/executor.py",
 line 244, in apply_migration
state = migration.apply(state, schema_editor)
  File 

Re: Adding a field to a model after initial migration

2016-01-29 Thread Guan Ke
When you run makemigrations command, Django will compare your current model 
module with the newest migration module. You should have a look at your 
0002_signup_updated... file to find out what cause the problem.

On Thursday, January 28, 2016 at 3:21:16 AM UTC+8, ofeyofey wrote:
>
> Hi,
> I forgot to add a field to a model and wish to add it now.
> I added the line to models.py but makemigrations and migrate aren't 
> working now.
> Is this possible in Django or do i need to go directly tp the sqlite DB 
> and do it using SQL?
> Thanks
> Currently following this tutorial 
> https://www.youtube.com/watch?v=g0tvJsUAx7g
>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/3148ce64-5d7f-40a9-ab5a-3de73371bd4a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Adding a field to a model after initial migration

2016-01-28 Thread Daniel Roseman
On Wednesday, 27 January 2016 21:29:18 UTC, ofeyofey wrote:
>
> No problem. Thanks for your help.
> Maybe fields can't be added to a model with django.
> I will try dropping the table in the DB and see what happens.
> Thanks again,
>

Adding fields to models is quite literally what migrations are for.
--
DR. 

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/7aa1bb2c-7ec3-4756-9a32-e6d3011743e4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Adding a field to a model after initial migration

2016-01-27 Thread ofeyofey
Alas too late... I have dropped the table.
But this link http://www.djangobook.com/en/2.0/chapter10.html
to a Django Book for version 1.0 may hold some ideas under the section 
about adding fields.
Thanks

On Wednesday, 27 January 2016 19:21:16 UTC, ofeyofey wrote:
>
> Hi,
> I forgot to add a field to a model and wish to add it now.
> I added the line to models.py but makemigrations and migrate aren't 
> working now.
> Is this possible in Django or do i need to go directly tp the sqlite DB 
> and do it using SQL?
> Thanks
> Currently following this tutorial 
> https://www.youtube.com/watch?v=g0tvJsUAx7g
>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/1d85f45e-0987-4a05-acfa-1fad9d1e3087%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Adding a field to a model after initial migration

2016-01-27 Thread sum abiut
You should try running it as root.

On Thu, Jan 28, 2016 at 8:29 AM, ofeyofey  wrote:

> No problem. Thanks for your help.
> Maybe fields can't be added to a model with django.
> I will try dropping the table in the DB and see what happens.
> Thanks again,
>
> On Wednesday, 27 January 2016 19:21:16 UTC, ofeyofey wrote:
>>
>> Hi,
>> I forgot to add a field to a model and wish to add it now.
>> I added the line to models.py but makemigrations and migrate aren't
>> working now.
>> Is this possible in Django or do i need to go directly tp the sqlite DB
>> and do it using SQL?
>> Thanks
>> Currently following this tutorial
>> https://www.youtube.com/watch?v=g0tvJsUAx7g
>>
> --
> 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 post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/3441fafe-fa06-47e8-b280-31e46d90251a%40googlegroups.com
> 
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAPCf-y5PERyLN-kzc%3Da0oV%3DO3a5iOHAnXReC4fCUvT2Vr9Od-A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Adding a field to a model after initial migration

2016-01-27 Thread ofeyofey
No problem. Thanks for your help.
Maybe fields can't be added to a model with django.
I will try dropping the table in the DB and see what happens.
Thanks again,

On Wednesday, 27 January 2016 19:21:16 UTC, ofeyofey wrote:
>
> Hi,
> I forgot to add a field to a model and wish to add it now.
> I added the line to models.py but makemigrations and migrate aren't 
> working now.
> Is this possible in Django or do i need to go directly tp the sqlite DB 
> and do it using SQL?
> Thanks
> Currently following this tutorial 
> https://www.youtube.com/watch?v=g0tvJsUAx7g
>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/3441fafe-fa06-47e8-b280-31e46d90251a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Adding a field to a model after initial migration

2016-01-27 Thread Xristos Xristoou
i dont know why sorry..try default value

Τη Τετάρτη, 27 Ιανουαρίου 2016 - 9:21:16 μ.μ. UTC+2, ο χρήστης ofeyofey 
έγραψε:
>
> Hi,
> I forgot to add a field to a model and wish to add it now.
> I added the line to models.py but makemigrations and migrate aren't 
> working now.
> Is this possible in Django or do i need to go directly tp the sqlite DB 
> and do it using SQL?
> Thanks
> Currently following this tutorial 
> https://www.youtube.com/watch?v=g0tvJsUAx7g
>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/b12e839f-908e-4235-894f-0d75e3b03124%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Adding a field to a model after initial migration

2016-01-27 Thread ofeyofey
Ok I put the app name which is 'news' after the commands for makemigrations 
and for migrate.
But I am still getting the same results.

On Wednesday, 27 January 2016 19:21:16 UTC, ofeyofey wrote:
>
> Hi,
> I forgot to add a field to a model and wish to add it now.
> I added the line to models.py but makemigrations and migrate aren't 
> working now.
> Is this possible in Django or do i need to go directly tp the sqlite DB 
> and do it using SQL?
> Thanks
> Currently following this tutorial 
> https://www.youtube.com/watch?v=g0tvJsUAx7g
>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/d2cdcf2c-bbca-46ce-971d-a2b9d8e0ce45%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Adding a field to a model after initial migration

2016-01-27 Thread Xristos Xristoou
you add your app name after the run makemigration and migrate ?if your app 
name is a blog then you run* makemigration blog *and *migrate blog*

Τη Τετάρτη, 27 Ιανουαρίου 2016 - 9:21:16 μ.μ. UTC+2, ο χρήστης ofeyofey 
έγραψε:
>
> Hi,
> I forgot to add a field to a model and wish to add it now.
> I added the line to models.py but makemigrations and migrate aren't 
> working now.
> Is this possible in Django or do i need to go directly tp the sqlite DB 
> and do it using SQL?
> Thanks
> Currently following this tutorial 
> https://www.youtube.com/watch?v=g0tvJsUAx7g
>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/287b857b-3097-49eb-a54a-d71bd04058c0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Adding a field to a model after initial migration

2016-01-27 Thread ofeyofey
Hi,

I changed the line to

updated = models.DateTimeField(auto_now_add = False, auto_now = True, null=
True)



But the same results,

[pi@Huawei-HG658c-instalacion src]$ python manage.py makemigrations
/home/pi/src/djangoTest/urls.py:20: RemovedInDjango110Warning: Support for 
string view arguments to url() is deprecated and will be removed in Django 
1.10 (got news.views.home). Pass the callable instead.
  url(r'^$', 'news.views.home', name='home'),

Migrations for 'news':
  0003_auto_20160127_2109.py:
- Alter field updated on signup
[pi@Huawei-HG658c-instalacion src]$ python manage.py migrate
/home/pi/src/djangoTest/urls.py:20: RemovedInDjango110Warning: Support for 
string view arguments to url() is deprecated and will be removed in Django 
1.10 (got news.views.home). Pass the callable instead.
  url(r'^$', 'news.views.home', name='home'),

Operations to perform:
  Apply all migrations: admin, news, contenttypes, auth, sessions
Running migrations:
  Rendering model states... DONE
  Applying news.0002_signup_updated...Traceback (most recent call last):
  File "manage.py", line 10, in 
...
  File 
"/usr/lib/python2.7/site-packages/django/db/models/fields/__init__.py", 
line 1399, in to_python
parsed = parse_datetime(value)
  File "/usr/lib/python2.7/site-packages/django/utils/dateparse.py", line 93
, in parse_datetime
match = datetime_re.match(value)
TypeError: expected string or buffer




Thanks

On Wednesday, 27 January 2016 19:21:16 UTC, ofeyofey wrote:
>
> Hi,
> I forgot to add a field to a model and wish to add it now.
> I added the line to models.py but makemigrations and migrate aren't 
> working now.
> Is this possible in Django or do i need to go directly tp the sqlite DB 
> and do it using SQL?
> Thanks
> Currently following this tutorial 
> https://www.youtube.com/watch?v=g0tvJsUAx7g
>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/79a24585-2e34-460c-8b94-5764eac4bad9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Adding a field to a model after initial migration

2016-01-27 Thread Xristos Xristoou
work now ?

Τη Τετάρτη, 27 Ιανουαρίου 2016 - 9:21:16 μ.μ. UTC+2, ο χρήστης ofeyofey 
έγραψε:
>
> Hi,
> I forgot to add a field to a model and wish to add it now.
> I added the line to models.py but makemigrations and migrate aren't 
> working now.
> Is this possible in Django or do i need to go directly tp the sqlite DB 
> and do it using SQL?
> Thanks
> Currently following this tutorial 
> https://www.youtube.com/watch?v=g0tvJsUAx7g
>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c9935dfd-ebdb-4f8a-849e-4e81027468c8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Adding a field to a model after initial migration

2016-01-27 Thread ofeyofey
Thanks for your reply.

The model.py contains,

class SignUp(models.Model):
email = models.EmailField()
full_name =  models.CharField(max_length = 120, blank = True, null = 
True)
timestamp = models.DateTimeField(auto_now_add = True, auto_now = False)
updated = models.DateTimeField(auto_now_add = False, auto_now = True)

def __unicode__(self): #__str__ for python3

  return self.email

I had to add the updated field,

updated = models.DateTimeField(auto_now_add = False, auto_now = True)

Running make migrations,

[pi@Huawei-HG658c-instalacion src]$ python manage.py makemigrations
/home/pi/src/djangoTest/urls.py:20: RemovedInDjango110Warning: Support for 
string view arguments to url() is deprecated and will be removed in Django 
1.10 (got news.views.home). Pass the callable instead.
  url(r'^$', 'news.views.home', name='home'),

No changes detected

Then running migrations

[pi@Huawei-HG658c-instalacion src]$ python manage.py migrate
/home/pi/src/djangoTest/urls.py:20: RemovedInDjango110Warning: Support for 
string view arguments to url() is deprecated and will be removed in Django 
1.10 (got news.views.home). Pass the callable instead.
  url(r'^$', 'news.views.home', name='home'),

Operations to perform:
  Apply all migrations: admin, news, contenttypes, auth, sessions
Running migrations:
  Rendering model states... DONE
  Applying news.0002_signup_updated...Traceback (most recent call last):
  File "manage.py", line 10, in 
execute_from_command_line(sys.argv)
  File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py"
, line 353, in execute_from_command_line
utility.execute()
  File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py"
, line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/lib/python2.7/site-packages/django/core/management/base.py", 
line 348, in run_from_argv
self.execute(*args, **cmd_options)
  File "/usr/lib/python2.7/site-packages/django/core/management/base.py", 
line 399, in execute
output = self.handle(*args, **options)
  File 
"/usr/lib/python2.7/site-packages/django/core/management/commands/migrate.py"
, line 200, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/usr/lib/python2.7/site-packages/django/db/migrations/executor.py", 
line 92, in migrate
self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=
fake_initial)
  File "/usr/lib/python2.7/site-packages/django/db/migrations/executor.py", 
line 121, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=
fake_initial)
  File "/usr/lib/python2.7/site-packages/django/db/migrations/executor.py", 
line 198, in apply_migration
state = migration.apply(state, schema_editor)
  File "/usr/lib/python2.7/site-packages/django/db/migrations/migration.py", 
line 123, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, 
project_state)
  File 
"/usr/lib/python2.7/site-packages/django/db/migrations/operations/fields.py"
, line 62, in database_forwards
field,
  File 
"/usr/lib/python2.7/site-packages/django/db/backends/sqlite3/schema.py", 
line 221, in add_field
self._remake_table(model, create_fields=[field])
  File 
"/usr/lib/python2.7/site-packages/django/db/backends/sqlite3/schema.py", 
line 103, in _remake_table
self.effective_default(field)
  File "/usr/lib/python2.7/site-packages/django/db/backends/base/schema.py", 
line 210, in effective_default
default = field.get_db_prep_save(default, self.connection)
  File 
"/usr/lib/python2.7/site-packages/django/db/models/fields/__init__.py", 
line 728, in get_db_prep_save
prepared=False)
  File 
"/usr/lib/python2.7/site-packages/django/db/models/fields/__init__.py", 
line 1461, in get_db_prep_value
value = self.get_prep_value(value)
  File 
"/usr/lib/python2.7/site-packages/django/db/models/fields/__init__.py", 
line 1440, in get_prep_value
value = super(DateTimeField, self).get_prep_value(value)
  File 
"/usr/lib/python2.7/site-packages/django/db/models/fields/__init__.py", 
line 1296, in get_prep_value
return self.to_python(value)
  File 
"/usr/lib/python2.7/site-packages/django/db/models/fields/__init__.py", 
line 1399, in to_python
parsed = parse_datetime(value)
  File "/usr/lib/python2.7/site-packages/django/utils/dateparse.py", line 93
, in parse_datetime
match = datetime_re.match(value)
TypeError: expected string or buffer

Thanks,




On Wednesday, 27 January 2016 19:21:16 UTC, ofeyofey wrote:
>
> Hi,
> I forgot to add a field to a model and wish to add it now.
> I added the line to models.py but makemigrations and migrate aren't 
> working now.
> Is this possible in Django or do i need to go directly tp the sqlite DB 
> and do it using SQL?
> Thanks
> Currently following this tutorial 
> https://www.youtube.com/watch?v=g0tvJsUAx7g
>

-- 
You received this message because you are subscribed to 

Re: Adding a field to a model after initial migration

2016-01-27 Thread Xristos Xristoou
no the problem is your new field affect models rules...try the new field to 
have null=True or default=somathing and then run on mynage.py makemigration 
yourapp and after migrate yourapp

christos

Τη Τετάρτη, 27 Ιανουαρίου 2016 - 9:21:16 μ.μ. UTC+2, ο χρήστης ofeyofey 
έγραψε:
>
> Hi,
> I forgot to add a field to a model and wish to add it now.
> I added the line to models.py but makemigrations and migrate aren't 
> working now.
> Is this possible in Django or do i need to go directly tp the sqlite DB 
> and do it using SQL?
> Thanks
> Currently following this tutorial 
> https://www.youtube.com/watch?v=g0tvJsUAx7g
>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/89327473-cf28-4aed-bee6-56f534136463%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Adding a field to a model after initial migration

2016-01-27 Thread sum abiut
What error did your get? what command did you use?

try
python manage.py makemigrations
python manage.py migrate

On Wed, Jan 27, 2016 at 11:21 AM, ofeyofey  wrote:

> Hi,
> I forgot to add a field to a model and wish to add it now.
> I added the line to models.py but makemigrations and migrate aren't
> working now.
> Is this possible in Django or do i need to go directly tp the sqlite DB
> and do it using SQL?
> Thanks
> Currently following this tutorial
> https://www.youtube.com/watch?v=g0tvJsUAx7g
>
> --
> 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 post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/fd23ad1b-19e9-404f-96da-df02262ab8f7%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAPCf-y6kTf5aFL4xjWHCaY-K-%2BRAkjYvYXRNH0Zkp_Y8Y8EJrA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Adding a field to a model after initial migration

2016-01-27 Thread ofeyofey
Hi,
I forgot to add a field to a model and wish to add it now.
I added the line to models.py but makemigrations and migrate aren't working 
now.
Is this possible in Django or do i need to go directly tp the sqlite DB and 
do it using SQL?
Thanks
Currently following this tutorial 
https://www.youtube.com/watch?v=g0tvJsUAx7g

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/fd23ad1b-19e9-404f-96da-df02262ab8f7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Error dynamically adding a field to a model in Django 1.7

2014-09-21 Thread Craig de Stigter
Hi folks

My app django-mptt has a function which dynamically adds a field to a 
model. A common use-case is hierarchical groups:

from mptt.models import TreeForeignKey, register
> from django.contrib.auth.models import Group
> TreeForeignKey(Group, blank=True, null=True).contribute_to_class(Group, 
> 'parent')
> register(Group)



This works great until Django 1.7. I've recently added 1.7 to the tox 
config and now I'm getting errors during the migration process:

django.db.utils.OperationalError: no such column: auth_group.parent_id


(full traceback here )

Seems the migration system isn't handling the monkey-patched field properly.

Does anyone know how to avoid this?

Cheers
Craig de Stigter

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/5aaf213c-f648-4d3d-818a-c64fe7fedfc1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Adding a field to a model

2007-03-24 Thread James Bennett

On 3/24/07, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote:
> (3) At your database prompt ("manage.py shell" and go from there),
> execute "ALTER TABLE ADD COLUMN ... DEFAULT ...", filling in the first
> "..." part with the column definition you noted before. You will also
> need to add a DEFAULT value to the column at the database level so that
> existing entries have a value for that column. If the column can be
> NULL, you won't need the default value -- it will just put NULLs in
> there for all the existing entries.

Alternative, and even better in my experience: write a SQL file which
will run the update inside a transaction, and commit that into the SVN
repo with your app so you have a history of what you've done to the
DB.

-- 
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Adding a field to a model

2007-03-24 Thread Malcolm Tredinnick

On Sat, 2007-03-24 at 18:04 -0700, Mark Engelberg wrote:
> I know that, unfortunately, there's no painless way to migrate all the
> data in your database when you add a new optional field to your model.
>  Pity.
> 
> So what is the MOST painless way to do this?

(1) Add the field to your model.

(2) Run "manage.py sql " and note the type of column that is
added to the model's table.

(3) At your database prompt ("manage.py shell" and go from there),
execute "ALTER TABLE ADD COLUMN ... DEFAULT ...", filling in the first
"..." part with the column definition you noted before. You will also
need to add a DEFAULT value to the column at the database level so that
existing entries have a value for that column. If the column can be
NULL, you won't need the default value -- it will just put NULLs in
there for all the existing entries.

(4) Sit back with drink of choice and realise just how painless the
whole process was.

Regards,
Malcolm



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Adding a field to a model

2007-03-24 Thread John M

I think the new .96 version has a export / import data function now.

On Mar 24, 6:04 pm, "Mark Engelberg" <[EMAIL PROTECTED]> wrote:
> I know that, unfortunately, there's no painless way to migrate all the
> data in your database when you add a new optional field to your model.
>  Pity.
>
> So what is the MOST painless way to do this?
>
> --Mark


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Adding a field to a model

2007-03-24 Thread Mark Engelberg

I know that, unfortunately, there's no painless way to migrate all the
data in your database when you add a new optional field to your model.
 Pity.

So what is the MOST painless way to do this?

--Mark

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: ProgrammingError ... after adding new field to my model ...

2006-03-19 Thread Kenneth Gonsalves

On Monday 20 Mar 2006 2:54 am, ZebZiggle wrote:
> For now, I did a u"My Unicode String".encode('ascii') and it
> works fine.

good to know - am planning a project with lots and lots of utf8 
stuff, so can avoid this error

-- 
regards
kg

http://www.livejournal.com/users/lawgon
tally ho! http://avsap.org.in
ಇಂಡ್ಲಿನಕ್ಸ வாழ்க!

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: ProgrammingError ... after adding new field to my model ...

2006-03-19 Thread ZebZiggle

SOLVED!

The text I was using for the value was unicode.

This resulted in the string not being quoted when the SQL was being
fabricated.

For now, I did a u"My Unicode String".encode('ascii') and it works
fine.

Cheers,
Zeb


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: ProgrammingError ... after adding new field to my model ...

2006-03-19 Thread ZebZiggle

Good point. Sadly, I'm not constructing the SQL, Django is. It could
very well be that postgres is mangling the value string (thinking it's
a column) ... that would possibly also explain why the exception
message is truncated. I was looking at the django code last night and
can't see anything obvious how it's mixing up the value with the column
name. Also, there is no explicit exception handling in the django code
that would be interfering with the error message.

It's on the INSERT side of the save() code (new record addition), I'm
going to try removing my new field again and see if the problem goes
away (I've tried everything else).

Really odd. 

Thx for the assistance! Keep the ideas comin'
-Z


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: ProgrammingError ... after adding new field to my model ...

2006-03-18 Thread Kenneth Gonsalves

On Sunday 19 Mar 2006 7:04 am, ZebZiggle wrote:
> I did a django-admin "sqlclear" followed by an "install"... as
> I've done every other time I modify my model. Is there something
> else I need to do?

the error message says the field is not there - inspect the database 
and check if it is there

-- 
regards
kg

http://www.livejournal.com/users/lawgon
tally ho! http://avsap.org.in
ಇಂಡ್ಲಿನಕ್ಸ வாழ்க!

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: ProgrammingError ... after adding new field to my model ...

2006-03-18 Thread Kenneth Gonsalves

On Sunday 19 Mar 2006 6:55 am, Kenneth Gonsalves wrote:
> On Sunday 19 Mar 2006 12:01 am, ZebZiggle wrote:
> > ERROR: relation "ph01990j" does not exist INSERT INTO
> > "mydarksecret_scriptelements"
> > ("script_id","scene","type","characterFirstName","characterLast
> >Na me","characterNickName","avatarFilename","isMurdeR
>
> you have added the field in the model but not in the database

you have to do this manually. Run python manage.py sqlall yourapp > 
somefile, and inspect somefile to see how django wants to render 
your new field, and then manually add the field to the table

-- 
regards
kg

http://www.livejournal.com/users/lawgon
tally ho! http://avsap.org.in
ಇಂಡ್ಲಿನಕ್ಸ வாழ்க!

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: ProgrammingError ... after adding new field to my model ...

2006-03-18 Thread ZebZiggle

I did a django-admin "sqlclear" followed by an "install"... as I've
done every other time I modify my model. Is there something else I need
to do?


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



Re: ProgrammingError ... after adding new field to my model ...

2006-03-18 Thread Kenneth Gonsalves

On Sunday 19 Mar 2006 12:01 am, ZebZiggle wrote:
> ERROR: relation "ph01990j" does not exist INSERT INTO
> "mydarksecret_scriptelements"
> ("script_id","scene","type","characterFirstName","characterLastNa
>me","characterNickName","avatarFilename","isMurdeR

you have added the field in the model but not in the database

-- 
regards
kg

http://www.livejournal.com/users/lawgon
tally ho! http://avsap.org.in
ಇಂಡ್ಲಿನಕ್ಸ வாழ்க!

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---



ProgrammingError ... after adding new field to my model ...

2006-03-18 Thread ZebZiggle

Hey guys,

(0.91 branch)

I recently added a new field to one of my model classes, now whenever I
attempt to save() a new record I get a ProgrammingError.

The call looks like this:

element = ScriptElement(script = script, scene = 0, type =
CHARACTER_ELEMENT, characterFirstName = firstName, characterLastName =
lastName, characterNickName = nickName, description = description,
isMurderer = isMurderer, avatarFilename = avatar)

element.save()

Where avatar = 'PH01990J.GIF' ... this gets changed later ...

and the Exception is:

ProgrammingError at /mydarksecret/makedebugdata/
ERROR: relation "ph01990j" does not exist INSERT INTO
"mydarksecret_scriptelements"
("script_id","scene","type","characterFirstName","characterLastName","characterNickName","avatarFilename","isMurdeR
Exception Type: ProgrammingError
Exception Value:ERROR: relation "ph01990j" does not exist INSERT INTO
"mydarksecret_scriptelements"
("script_id","scene","type","characterFirstName","characterLastName","characterNickName","avatarFilename","isMurdeR
Exception Location:

c:\python24\lib\site-packages\django-0.91-py2.4.egg\django\core\db\base.py
in execute, line 9

(The full exception message is getting truncated ... is there a way to
see the whole message?)

The 'ph01990j' is some modified form of 'PH01190.GIF' ... so something
seems to be getting confused now that this new field is added.

avatarFilename is just:
avatarFilename = meta.CharField(maxlength = 64, core = True)

Any thoughts where the mangling is occuring?

Thx,
Zeb


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~--~~~~--~~--~--~---