Re: restore migration files

2018-06-06 Thread Leif
thank you, Mike

On Wednesday, June 6, 2018 at 9:24:24 AM UTC-4, Mike Dewhirst wrote:
>
> Maybe you can find an old backup of your models and create a new database 
> from them. Then overwrite the old models with your latest ones and make 
> migrations again. Those should suffice to replace the deleted migrations.
>
> *Connected by Motorola*
>
>
> Leif > wrote:
>
> Dear Django Experts:
>
> Somehow I deleted the files under migrations directory. Now I am no longer 
> able to migrate new changes. I got 'No change detected' message when I 
> apply python manage.py migrate. Anway I can restore files under migrations 
> directory?
>
> Best,
>
> Leif
>
> -- 
> 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...@googlegroups.com .
> To post to this group, send email to django...@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/7c972a46-5e24-44cb-bb27-b0b006197d8e%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/django-users/7c972a46-5e24-44cb-bb27-b0b006197d8e%40googlegroups.com?utm_medium=email_source=footer>
> .
> 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/a2036314-2e4b-4437-9027-4e2546e0cc6e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: restore migration files

2018-06-06 Thread Leif
Hi Kirby,
Thank you very much. That is very helpful. It is working now. Now the 
migrations directory is tracked in code repository to prevent from 
happening again.
Best,
Leif  

On Wednesday, June 6, 2018 at 10:23:01 AM UTC-4, C. Kirby wrote:
>
> If you don't need the history of the app migrations then it is pretty 
> straightforward, you will basically end up with a squashed migration. If 
> you do need the migration history (there is another system that is not up 
> to sync with yours, etc) you are stuck, and I would suggest you start using 
> a version control system (git, hg, etc)
> Assuming it is ok to end up with one squashed migration:
>
> 1) Remove any changes you have made in your code that inform the database 
> schema for the app(s) in question(so model changes)
> 2) Access the database directly (assuming postgres - psql, other databases 
> have different interfaces)
> 3) There is a table django_migrations with the fields id, app, name, 
> applied. Run a 
> delete from django_migrations where app = '';
> for whatever app you deleted the migrations from (run it a few times if 
> you deleted multiple app migrations)
> 4) For all the apps you need to rebuild migrations for
> ./manage makemigrations [app-names]
> 5)For  each app that you needed to rebuild the migrations
> ./mange migrate [app-name] --fake
> This tells the database to write to the dajngo_migrations table that the 
> migration was run, but doesn't actually change the schema. This is why your 
> code and db need to be in sync. Otherwise you will have changes in the 
> migration that are not reflected in the db, but the migtration is marked as 
> having been run
>
> You are now back in shape. Now set up git. I  am happy to help or point 
> you to references for it
>
> Kirby
>
> On Wednesday, June 6, 2018 at 8:54:48 AM UTC-4, Leif wrote:
>>
>> Dear Django Experts:
>>
>> Somehow I deleted the files under migrations directory. Now I am no 
>> longer able to migrate new changes. I got 'No change detected' message when 
>> I apply python manage.py migrate. Anway I can restore files under 
>> migrations directory?
>>
>> Best,
>>
>> Leif
>>
>>

-- 
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/2afc9851-776a-487c-bf83-d038bc5c769e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: restore migration files

2018-06-06 Thread Leif
there is a suggestion here but not clear:
https://stackoverflow.com/questions/34919650/deleted-migration-files-how-to-makemigrations-without-losing-data


On Wednesday, June 6, 2018 at 8:54:48 AM UTC-4, Leif wrote:
>
> Dear Django Experts:
>
> Somehow I deleted the files under migrations directory. Now I am no longer 
> able to migrate new changes. I got 'No change detected' message when I 
> apply python manage.py migrate. Anway I can restore files under migrations 
> directory?
>
> Best,
>
> Leif
>
>

-- 
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/cd9ff30c-c02a-4e75-b8d7-2c4dfd26e5bb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


restore migration files

2018-06-06 Thread Leif
Dear Django Experts:

Somehow I deleted the files under migrations directory. Now I am no longer 
able to migrate new changes. I got 'No change detected' message when I 
apply python manage.py migrate. Anway I can restore files under migrations 
directory?

Best,

Leif

-- 
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/7c972a46-5e24-44cb-bb27-b0b006197d8e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: django restframework relation

2018-03-14 Thread Leif
Hi Jason,
Thank you fo rthe quick reply.

Did you mean to create XyzSerializer like this?

class XyzSerializer(serializers.ModelSerializer):
tracks = TrackSerializer(many=True, read_only=True)

when you said "do the same in TrackSerializer" did you mean 'reverse 
relation'?
How to do reverse relation in this case?

Best,
Leif

On Wednesday, March 14, 2018 at 11:14:49 AM UTC-4, Leif wrote:
>
> Dear Django,
>
> I have a question about django rest framework relations. I am looking at 
> the example in 
> http://www.tomchristie.com/rest-framework-2-docs/api-guide/relations. In 
> my situation there is additional class called Xyz:
>
> class Xyz(models.Model):
> xyz_name = models.CharField(max_length=100)
> name2 = models.CharField(max_length=100)
>
> class Album(models.Model):
> album_name = models.CharField(max_length=100)
> artist = models.CharField(max_length=100)
>
> class Track(models.Model):
> album = models.ForeignKey(Album, related_name='tracks')
> xyz = models.ForeignKey(Xyz, related_name=‘xyz’)
> order = models.IntegerField()
> title = models.CharField(max_length=100)
> duration = models.IntegerField()
>
> here is the example of serializer in the link above. 
>
> class TrackSerializer(serializers.ModelSerializer):
> class Meta:
> model = Track
> fields = ('order', 'title')
>
> class AlbumSerializer(serializers.ModelSerializer):
> tracks = TrackSerializer(many=True, read_only=True)
>
> class Meta:
> model = Album
> fields = ('album_name', 'artist', 'tracks')
>
> My question is how to include XyzSerializer(serializers.ModelSerializer) 
> so all three objects will be included in json output.
>
> Thank you very much in advance,
>
> Leif
>
>

-- 
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/029061f7-10ad-4681-a8b5-7900262bdeb0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


django restframework relation

2018-03-14 Thread Leif
Dear Django,

I have a question about django rest framework relations. I am looking at 
the example in 
http://www.tomchristie.com/rest-framework-2-docs/api-guide/relations. In my 
situation there is additional class called Xyz:

class Xyz(models.Model):
xyz_name = models.CharField(max_length=100)
name2 = models.CharField(max_length=100)

class Album(models.Model):
album_name = models.CharField(max_length=100)
artist = models.CharField(max_length=100)

class Track(models.Model):
album = models.ForeignKey(Album, related_name='tracks')
xyz = models.ForeignKey(Xyz, related_name=‘xyz’)
order = models.IntegerField()
title = models.CharField(max_length=100)
duration = models.IntegerField()

here is the example of serializer in the link above. 

class TrackSerializer(serializers.ModelSerializer):
class Meta:
model = Track
fields = ('order', 'title')

class AlbumSerializer(serializers.ModelSerializer):
tracks = TrackSerializer(many=True, read_only=True)

class Meta:
model = Album
fields = ('album_name', 'artist', 'tracks')

My question is how to include XyzSerializer(serializers.ModelSerializer) so 
all three objects will be included in json output.

Thank you very much in advance,

Leif

-- 
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/fecb9167-cfa1-49e6-8af4-7f3b56860f23%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: loading script deployment

2018-02-11 Thread Leif
Thank you, Jani,
I will try.
Best,
Leif

On Saturday, February 10, 2018 at 10:14:29 AM UTC-5, Leif wrote:
>
> Dear Django,
>
> Sorry if this question has been answered. I have a Django based web 
> application and I just developed a loading script to upload data to 
> database using django model. The code is located under management/commands. 
> Here is the commands to run the script:
>
> $source ./appenv/bin/activate appenv
> (appenv)$python manage.py load_data data_file.csv
>
> I would like to deploy this script to my client. I don't want to send the 
> entire code of web application. How do I do it? 
>
> Thank you in advance for your help.
>
> Best,
>
> Leif
>

-- 
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/55817374-92fb-4a8e-81ff-d239158793c3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: loading script deployment

2018-02-10 Thread Leif
Hi Dylan,
Thank you for the great input. Hopefully django REST interface will be able 
to handle the post for large amount of data (~500k rows) without problem.
Best,
Leif 

On Saturday, February 10, 2018 at 10:14:29 AM UTC-5, Leif wrote:
>
> Dear Django,
>
> Sorry if this question has been answered. I have a Django based web 
> application and I just developed a loading script to upload data to 
> database using django model. The code is located under management/commands. 
> Here is the commands to run the script:
>
> $source ./appenv/bin/activate appenv
> (appenv)$python manage.py load_data data_file.csv
>
> I would like to deploy this script to my client. I don't want to send the 
> entire code of web application. How do I do it? 
>
> Thank you in advance for your help.
>
> Best,
>
> Leif
>

-- 
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/4413e8af-6203-47e9-9537-720f224e2873%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


loading script deployment

2018-02-10 Thread Leif
Dear Django,

Sorry if this question has been answered. I have a Django based web 
application and I just developed a loading script to upload data to 
database using django model. The code is located under management/commands. 
Here is the commands to run the script:

$source ./appenv/bin/activate appenv
(appenv)$python manage.py load_data data_file.csv

I would like to deploy this script to my client. I don't want to send the 
entire code of web application. How do I do it? 

Thank you in advance for your help.

Best,

Leif

-- 
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/08fbd5c7-7b1f-410d-af06-7a5e7802b0e5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Custom admin field widgets in the newforms-admin branch

2007-06-27 Thread leif strickland

After some digging around, it looks like the newforms-admin branch
does not allow widget declarations within the model class itself.
However, adding a custom widget is quite easy.

In the branch, almost all of the admin app functionality has been
moved out of the inner Admin class and into a separate class called
ModelAdmin. If you want to customize the admin app's behavior, simply
subclass ModelAdmin and override the default functionality.

For example, look at this sample ModelAdmin subclass:

-

from django.contrib import admin

[...]

class ProfileOptions(admin.ModelAdmin):
list_display = ('display_name', 'headline','setup_date')
filter_horizontal = ('techniques', 'products', 'setup')
fields = (('Administrative Settings',{'fields':
('subscriber','active')}),('Contact Information',{'fields':
('phone_numbers','im_accounts','zip_code')}),('Photos',{'fields':
('primary_photo','secondary_photos','private_photos')}),)

def formfield_for_dbfield(self, db_field, **kwargs):
if db_field.name == 'zip_code':
return ZipCodeField(**kwargs)
else:
return 
super(ProfileOptions,self).formfield_for_dbfield(db_field,
**kwargs)

def queryset(self, request):
return self.model.user_profiles.get_query_set()


adminsite.register(Profile,ProfileOptions)




list_display and fields are used exactly as they were in inner Admin
classes. filter_horizontal is simply a tuple of fields that will use
the javascript multiple-select filter.

The queryset() function allows you to specify which manager should be
used to obtain the change list objects.

Then there's formfield_for_dbfield(), a function that allows you to
override the default form field types that are returned for your
models' fields. In this example, the function returns a custom field
class (ZipCodeField) for any model field named "zip_code".

Alternatively, you could specify a custom widget like so:

if db_field.name == 'zip_code':
kwargs['widget'] = WIDGET
return 
super(ProfileOptions,self).formfield_for_dbfield(db_field,
**kwargs)

Finally, don't forget to register your ModelAdmin subclass for the
model.

You can find more information on the branch's homepage:

http://code.djangoproject.com/wiki/NewformsAdminBranch

I hope this helps! If I am wrong about any of this, please post so
others don't repeat my mistakes.

Cheers,
LS



--~--~-~--~~~---~--~~
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: Announcing the birth of Fluther.com

2007-06-27 Thread leif strickland

Looks great. Good work!

For those of us still stuck in alpha, can you share a bit about your
implementation? Are you using Apache? Squid?  What's your approach to
caching? Did you use Django's built-in User model for your registered
site users? What DB did you opt for?

Any information would be a great help to all the Django newbies. Plus,
you might get some valuable feedback from the pros on this board.

Again, congrats!


--~--~-~--~~~---~--~~
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: Zip code lookup field

2007-06-27 Thread leif strickland

I figured it out!

The problem was that my custom zip code field's clean() function was
returning the profile object's *id*, not the actual zip code
instance.

>  zip_code_id = Postal_Code.objects.get(postal_code=value).id
>  return int(zip_code_id)

As it turns out, the function should have returned the actual object.
All I had to do to make this code work, therefore, was remove the
".id" from the end of the first line above. Voila!

Lesson learned: Custom fields for foreign keys must return an
instance, not the key.

Thanks again for the help. Hope this thread helps someone else in the
future.

Cheers,
LS



--~--~-~--~~~---~--~~
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: Zip code lookup field

2007-06-27 Thread leif strickland

Here's a bit of the testing I have run in the shell...
>>> from mysite2.profile.models import Profile
>>> from django import newforms
>>> from mysite2.admin import ProfileOptions
>>> # instantiate a Profile object
>>> test_profile = Profile.objects.get(id=1)

>>> # instantiate the adminoptions class
>>> # used by the admin site
>>> options = ProfileOptions(Profile)

>>> # create a form class for the profile instance
>>> form = newforms.form_for_instance(test_profile,formfield_callback = 
>>> options.formfield_for_dbfield)
>>> form



>>> # check that the zip_code field is of type ZipCodeField
>>> form.base_fields['zip_code']



>>> # in my database, i currently have only one zip code
>>> # 90069, with a key of 1.

>>> form.base_fields['zip_code'].initial
1

>>> form.base_fields['zip_code'].clean(value='90069')
1

>>> form.base_fields['zip_code'].clean(value='111')


Traceback (most recent call last):
  File "", line 1, in ?
  File "Sites/Django/mysite2/../mysite2/admin.py", line 40, in clean
raise ValidationError, "Please enter a valid zip code."
ValidationError: [u'Please enter a valid zip code.']
>>> form.base_fields['zip_code'].clean(value='')


Traceback (most recent call last):
  File "", line 1, in ?
  File "/Users/leifstrickland/Sites/DjangoProject/mysite2/../mysite2/
admin.py", line 40, in clean
raise ValidationError, "Please enter a valid zip code."
ValidationError: [u'Please enter a valid zip code.']
>>> zip_code_field = form.base_fields['zip_code']
>>> zip_code_field.widget.render(name='zip_code',value=1)


u''
>>> zip_code_field.widget.render(name='zip_code',value='')


u''
As you can see, everything seems to be working. I'm so puzzled...



--~--~-~--~~~---~--~~
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: Zip code lookup field

2007-06-27 Thread leif strickland

Here's a bit of the testing I have run in the shell...


>>> from mysite2.profile.models import Profile
>>> from django import newformms
>>> from mysite2.admin import ProfileOptions
>>>
>>> # instantiate a Profile object
>>> test_profile = Profile.objects.get(id=1)
>>>
>>> # instantiate the adminoptions class
>>> # used by the admin site
>>> options = ProfileOptions(Profile)
>>>
>>> # create a form class for the profile instance
>>> form = newforms.form_for_instance(test_profile,formfield_callback = 
>>> options.formfield_for_dbfield)
>>> form

>>>
>>> # check that the zip_code field is of type ZipCodeField
>>> form.base_fields['zip_code']

>>>
>>> # in my database, i currently have only one zip code
>>> # 90069, with a key of 1.
>>>
>>> form.base_fields['zip_code'].initial
1
>>>
>>> form.base_fields['zip_code'].clean(value='90069')
1
>>>
>>> form.base_fields['zip_code'].clean(value='111')
Traceback (most recent call last):
  File "", line 1, in ?
  File "Sites/Django/mysite2/../mysite2/admin.py", line 40, in clean
raise ValidationError, "Please enter a valid zip code."
ValidationError: [u'Please enter a valid zip code.']
>>>
>>> form.base_fields['zip_code'].clean(value='')
Traceback (most recent call last):
  File "", line 1, in ?
  File "/Users/leifstrickland/Sites/DjangoProject/mysite2/../mysite2/
admin.py", line 40, in clean
raise ValidationError, "Please enter a valid zip code."
ValidationError: [u'Please enter a valid zip code.']
>>>
>>> zip_code_field = form.base_fields['zip_code']
>>> zip_code_field.widget.render(name='zip_code',value=1)
u''
>>>
>>> zip_code_field.widget.render(name='zip_code',value='')
u''

As you can see, everything seems to be working. I'm so puzzled...




--~--~-~--~~~---~--~~
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: Zip code lookup field

2007-06-27 Thread leif strickland

Yes, the ModelAdmin is registered with django.contrib.admin.site. And
the change and add pages for the model are displaying correctly. It
even provides the expected error message if I enter an invalid Zip
code.
But when I enter a correct Zip code and Save, it gives me an
Integrity
Error:

---

IntegrityError at /admin/profile/profile/2/
null value in column "zip_code_id" violates not-null constraint

TRACEBACK

/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-
packages/django/core/handlers/base.py in get_response
response = callback(request, *callback_args,
**callback_kwargs) ...
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-
packages/django/contrib/admin/sites.py in root
return self.model_page(request, *url.split('/', 2)) ...
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-
packages/django/contrib/admin/sites.py in model_page
return admin_obj(request, rest_of_url) ...
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-
packages/django/contrib/admin/options.py in __call__
return self.change_view(request, unquote(url)) ...
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-
packages/django/contrib/admin/options.py in change_view
return self.save_change(request, model, form,
inline_formsets) ...
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-
packages/django/contrib/admin/options.py in save_change
new_object = form.save(commit=True) ...
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-
packages/django/newforms/models.py in save
return save_instance(self, instance, fields, fail_message,
commit) ...
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-
packages/django/newforms/models.py in save_instance
instance.save() ...
/Sites/Django/mysite2/../mysite2/profile/models.py in save
super(Profile, self).save()  ...
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-
packages/django/db/models/base.py in save
db_values + [pk_val]) ...
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-
packages/django/db/backends/util.py in execute
return self.cursor.execute(sql, params) ...

---

In the last step, the local db_values var is a tuple of values to be
used in the DB query. All values are correct except for the
zip_code_id, which is None. That shouldn't be the case, because the
zip code field's clean() function has worked properly (i.e., returned
the corresponding zip_code_id) in shell and admin testing. So somehow
the value returned by the clean() function is being overwritten or
ignored later in the script. I just can't figure it out.

Thanks again for all the help.



--~--~-~--~~~---~--~~
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: Zip code lookup field

2007-06-27 Thread leif strickland

Yes, the ModelAdmin is registered with django.contrib.admin.site. And
the change and add pages for the model are displaying correctly. It
even provides the expected error message if I enter an invalid Zip
code.

But when I enter a correct Zip code and Save, it gives me an Integrity
Error:

---

IntegrityError at /admin/profile/profile/2/
null value in column "zip_code_id" violates not-null constraint

TRACEBACK

/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-
packages/django/core/handlers/base.py in get_response
response = callback(request, *callback_args,
**callback_kwargs) ...
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-
packages/django/contrib/admin/sites.py in root
return self.model_page(request, *url.split('/', 2)) ...
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-
packages/django/contrib/admin/sites.py in model_page
return admin_obj(request, rest_of_url) ...
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-
packages/django/contrib/admin/options.py in __call__
return self.change_view(request, unquote(url)) ...
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-
packages/django/contrib/admin/options.py in change_view
return self.save_change(request, model, form,
inline_formsets) ...
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-
packages/django/contrib/admin/options.py in save_change
new_object = form.save(commit=True) ...
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-
packages/django/newforms/models.py in save
return save_instance(self, instance, fields, fail_message,
commit) ...
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-
packages/django/newforms/models.py in save_instance
instance.save() ...
/Users/leifstrickland/Sites/DjangoProject/mysite2/../mysite2/profile/
models.py in save
super(Profile, self).save()  ...
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-
packages/django/db/models/base.py in save
db_values + [pk_val]) ...
/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-
packages/django/db/backends/util.py in execute
return self.cursor.execute(sql, params) ...

---

In the last step, the local db_values var is a tuple of values to be
used in the DB query. All values are correct except for the
zip_code_id, which is None. That shouldn't be the case, because the
zip code field's clean() function has worked properly (i.e., returned
the corresponding zip_code_id) in shell and admin testing. So somehow
the value returned by the clean() function is being overwritten or
ignored later in the script. I just can't figure it out.

Thanks again for all the help.


--~--~-~--~~~---~--~~
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: Zip code lookup field

2007-06-27 Thread leif strickland

OK, I've been tinkering with this all day, and I'm still having one
big problem
As Jeremy suggested, I created custom ZipCodeField and MyAdmin
classes. In addition, I created a custom ZipcodeLookupInput widget.

I've tested this in the shell and on the test server, and the clean
and render functions appear to be working beautifully. The widget's
render() function looks up the initial value (the zip code id) and
uses the corresponding zip code as its display value, and the field's
clean() function converts the user-provided zip into an id and
returns
it as an integer value.

Unfortunately, I keep getting an error when I try to save. It appears
that the zip_code_id is None by the time a DB query is attempted in
django.db.backends.util. But this makes no sense, beacuse the clean()
function seems to be working properly (i.e., it returns an integer
value).

Does anyone know what might be happening?  I have pasted my custom
classes code below.

from mysite.address.models import Postal_Code
from django import newforms as forms

[...]

class ProfileOptions(admin.ModelAdmin):
def formfield_for_dbfield(self, db_field, **kwargs):
if db_field.name == 'zip_code':
return ZipCodeField(**kwargs)
else:
return
super(ProfileOptions,self).formfield_for_dbfield(db_field,
**kwargs)

class ZipCodeField(forms.fields.CharField):
def __init__(self, **kwargs):
kwargs['widget'] = ZCLookupTextInput
self.max_length, self.min_length = 5, 5
super(ZipCodeField, self).__init__(**kwargs)

def clean(self, value):
super(ZipCodeField, self).clean(value)
try:
zip_code_id =
Postal_Code.objects.get(postal_code=value).id
except Postal_Code.DoesNotExist:
raise ValidationError, "Please enter a valid
zip code."
return int(zip_code_id)

class ZCLookupTextInput(forms.widgets.TextInput):
def render(self, name, value, attrs=None):
try:
zip_code = Postal_Code.objects.get(id =
value).postal_code
except:
zip_code = value
return super(ZCLookupTextInput, self).render(name,
zip_code, attrs)



--~--~-~--~~~---~--~~
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: Zip code lookup field

2007-06-27 Thread leif strickland

OK, I've been tinkering with this all day, and I'm still having one
big problem

As Jeremy suggested, I created custom ZipCodeField and MyAdmin
classes. In addition, I created a custom ZipcodeLookupInput widget.

I've tested this in the shell and on the test server, and the clean
and render functions appear to be working beautifully. The widget's
render() function looks up the initial value (the zip code id) and
uses the corresponding zip code as its display value, and the field's
clean() function converts the user-provided zip into an id and returns
it as an integer value.

Unfortunately, I keep getting an error when I try to save. It appears
that the zip_code_id is None by the time a DB query is attempted in
django.db.backends.util. But this makes no sense, beacuse the clean()
function seems to be working properly (i.e., it returns an integer
value).

Does anyone know what might be happening?  I have pasted my custom
classes code below.


from mysite.address.models import Postal_Code
from django import newforms as forms

[...]

class ProfileOptions(admin.ModelAdmin):
def formfield_for_dbfield(self, db_field, **kwargs):
if db_field.name == 'zip_code':
return ZipCodeField(**kwargs)
else:
return 
super(ProfileOptions,self).formfield_for_dbfield(db_field,
**kwargs)

class ZipCodeField(forms.fields.CharField):
def __init__(self, **kwargs):
kwargs['widget'] = ZCLookupTextInput
self.max_length, self.min_length = 5, 1
super(ZipCodeField, self).__init__(**kwargs)

def clean(self, value):
super(ZipCodeField, self).clean(value)
try:
zip_code_id = 
Postal_Code.objects.get(postal_code=value).id
except Postal_Code.DoesNotExist:
raise ValidationError, "Please enter a valid zip code."
return int(zip_code_id)


class ZCLookupTextInput(forms.widgets.TextInput):
def render(self, name, value, attrs=None):
try:
zip_code = Postal_Code.objects.get(id = 
value).postal_code
except:
zip_code = value
return super(LookupTextInput, self).render(name, zip_code, 
attrs)




--~--~-~--~~~---~--~~
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: Custom admin field widgets in the newforms-admin branch

2007-06-27 Thread leif

Does anyone have information on this topic? I have a feeling I'm not
the only one who's wondering this. Thanks!


--~--~-~--~~~---~--~~
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: Zip code lookup field

2007-06-27 Thread leif

Great advice -- thanks! I'll work on it today and let you know how it
comes together.


--~--~-~--~~~---~--~~
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: Zip code lookup field

2007-06-27 Thread leif

> Django's existing admin uses oldforms.  The newforms-admin branch is
> working to rewrite admin to be more flexible and use newforms.  If you
> need it on trunk now, you'll need to use oldforms.

Thanks for the input, Jeremy. I've actually decided to use newforms-
admin since I won't be going to production for another few months (and
hopefully it will be incorporated into the trunk soon). Do you know
how I can attack the problem using that branch?


--~--~-~--~~~---~--~~
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: Zip code lookup field

2007-06-26 Thread leif

Very good point, Tim. I'll consider that issue as I move forward with
my application.

My main problem, though, is figuring out how to create a text input
lookup field for the ZIP code foreign key. Do I need to code a new
widget? A new form field class? A function to hook into newforms'
validation?

And if I create a widget or form field class, how to I coax Django
into using it for the admin change and add pages?



--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Zip code lookup field

2007-06-26 Thread leif

Hi there. I'm new to Django and Python, so I appreciate any help you
guys can provide.

I am working on an app that allows a user to create extended profiles
with text and photos. In addition, each profile is associated with a
single ZIP code via a ForeignKey.

As it stands now, the admin add-profile page generates a select
pulldown menu with all available ZIP codes. That, of course, cannot
work on a production site. Instead, I would like to have a text field
that accepts a user's ZIP code and then performs a lookup on the
foreign key during validation. If the given ZIP is found, the
associated foreign key would be saved; if not, an error would be
returned.

I know I theoretically could create a ZIP code table in which the
codes themselves were the primary keys, and then use raw_admin_id. But
I will be working with a legacy with previously-assigned keys, and ZIP
codes aren't really well suited for primary keys, anyway.

My question, then, is this: What's the most elegant way to pull off
this hack? By the way, I am using newforms and the newforms-admin
branch. (I want my customizations to work with 1.0 and beyond.)

Thanks for your help!


--~--~-~--~~~---~--~~
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: Custom admin field widgets in the newforms-admin branch

2007-06-26 Thread leif

By the way, I did find the hook in django/branches/newforms-admin/
django/contrib/admin/options.py for specifying the form field for a
given database field. But that's different than allowing a developer
to specify the widget for a given field in the model itself.



--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---