Re: Where exactly in the django source code, the default permissions are created?

2013-12-20 Thread Felipe Coelho
2013/12/19 Fabio C. Barrionuevo da Luz 

> Hello guys, I'm trying to understand the inner workings of the permissions
> django system.
>
> My question is where exactly in the django source code, the default
> permissions are created?
>

Default permissions are created by
django.contrib.auth.management.create_permissions [1], which is called by
the post_syncdb signal [2]

https://github.com/django/django/blob/stable/1.6.x/django/contrib/auth/management/__init__.py#L61
https://github.com/django/django/blob/stable/1.6.x/django/contrib/auth/management/__init__.py#L190

-- 
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/CAP5dYfND%3D18w73OVKWQXDw-mJwrUmMSxvV3M6bG1ef37TrBYHg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Problems with default settings.py file

2013-12-02 Thread Felipe Coelho
2013/12/2 Blake Adams 

> Hello I am a python noob going through the getttingstarted with django
> tutorial.
>
> For some reason When I start a new project, the default settings.py file
> is missing a lot of default settings (Admin, Media, Some of the Databse
> info, Statis_file, Logging, etc.).  Im looking for any direction on why
> this is happening and what I might be doing wrong.  Thanks!
>
> Here is my current complete settings file below (version that gets created
> with missing info):
>
> """
> Django settings for microblog2 project.
>
> For more information on this file, see
> https://docs.djangoproject.com/en/1.6/topics/settings/
>
> For the full list of settings and their values, see
> https://docs.djangoproject.com/en/1.6/ref/settings/
> """
>
> # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
> import os
> BASE_DIR = os.path.dirname(os.path.dirname(__file__))
>
>
> # Quick-start development settings - unsuitable for production
> # See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/
>
> # SECURITY WARNING: keep the secret key used in production secret!
> SECRET_KEY =
>
> # SECURITY WARNING: don't run with debug turned on in production!
> DEBUG = True
>
> TEMPLATE_DEBUG = True
>
> ALLOWED_HOSTS = []
>
>
> # Application definition
>
> INSTALLED_APPS = (
> 'django.contrib.admin',
> 'django.contrib.auth',
> 'django.contrib.contenttypes',
> 'django.contrib.sessions',
> 'django.contrib.messages',
> 'django.contrib.staticfiles',
> )
>
> MIDDLEWARE_CLASSES = (
> 'django.contrib.sessions.middleware.SessionMiddleware',
> 'django.middleware.common.CommonMiddleware',
> 'django.middleware.csrf.CsrfViewMiddleware',
> 'django.contrib.auth.middleware.AuthenticationMiddleware',
> 'django.contrib.messages.middleware.MessageMiddleware',
> 'django.middleware.clickjacking.XFrameOptionsMiddleware',
> )
>
> ROOT_URLCONF = 'microblog2.urls'
>
> WSGI_APPLICATION = 'microblog2.wsgi.application'
>
>
> # Database
> # https://docs.djangoproject.com/en/1.6/ref/settings/#databases
>
> DATABASES = {
> 'default': {
> 'ENGINE': 'django.db.backends.sqlite3',
> 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
> }
> }
>
> # Internationalization
> # https://docs.djangoproject.com/en/1.6/topics/i18n/
>
> LANGUAGE_CODE = 'en-us'
>
> TIME_ZONE = 'UTC'
>
> USE_I18N = True
>
> USE_L10N = True
>
> USE_TZ = True
>
>
> # Static files (CSS, JavaScript, Images)
> # https://docs.djangoproject.com/en/1.6/howto/static-files/
>
> STATIC_URL = '/static/'
>

They are not "missing", they are just not specified. In 1.5, a lot of
settings were present in the default settings file but they only contained
the default values for those settings, you could easily remove them and
nothing would change.

1.6 simplified [1] the default project template, it's a lot less confusing
for someone who's just starting with Django, and if you need the extras,
take a look at the settings reference [2].

[1]
https://docs.djangoproject.com/en/dev/releases/1.6/#simplified-default-project-and-app-templates
[2] https://docs.djangoproject.com/en/1.6/ref/settings/

-- 
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/CAP5dYfOrKVzL9viQb3FMN%2BwxJAfFqEEKbgesbEZg%3D%3DW26pv2RQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Email Templates and the full website URL

2013-11-29 Thread Felipe Coelho
2013/11/29 Joseph Mutumi 

> That could work but isn't it a bit insecure? I think it will be
> susceptible to a header injection(
> http://en.wikipedia.org/wiki/HTTP_header_injection). I would rather
> create a setting with the domain name in settings.py and then call it from
> the template or write a custom template tag.
>

Check Django's ALLOWED_HOSTS [1] setting, it is supposed to account for
this, and Django 1.5+ requires you to explicitly set it in order to run a
site with DEBUG=False

[1]
https://docs.djangoproject.com/en/1.6/topics/security/#host-header-validation

-- 
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/CAP5dYfP%2BJgrDnnN_aCNRwdaO8bQbkRB36eUQJef8g4T5BRj_Cg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: django tests

2013-11-23 Thread Felipe Coelho
2013/11/23 Harjot Mann 

> On Sat, Nov 23, 2013 at 2:49 PM, Simone Dalla  wrote:
> > the correct code should be
> >
> > ...
> > self.userprofile = UserProfile.objects.filter(user_id =1)
> > self.userprofile.update(first_name = "Harjot")
> > ...
> > self.assertEqual(self.userprofile.user_id, 1)
>
> New Error.
> Traceback (most recent call last):
>   File "/home/harjot/Automation/../Automation/tcc/tests.py", line 22,
> in test_profile
> self.assertEqual(self.userprofile.user_id, 1)
> AttributeError: 'QuerySet' object has no attribute 'user_id'
>

filter() [1] returns a QuerySet, if you want and are *absolutely sure* that
your query will return a single object, use get() [2] instead, possibly
with a try/catch block.

[1]
https://docs.djangoproject.com/en/1.6/ref/models/querysets/#django.db.models.query.QuerySet.filter
[2]
https://docs.djangoproject.com/en/1.6/ref/models/querysets/#django.db.models.query.QuerySet.get

-- 
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/CAP5dYfNz0wvO6Twa2MtX-e5C38-Cm5Ewykn%3Dgvr3qd2E_ZGbbA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: Tango with Django Tutorials

2013-11-22 Thread Felipe Coelho
On Nov 22, 2013 7:01 AM, "Amimo Benja"  wrote:
>
> Does anyone know how I can convert the entire Tango with Django Tutorials
from online to an offline website that I can store and use on my hard disk?
Please help I desperately need to use this site while am offline

The book is open source, just download the sources and make your own local
copy with sphinx

https://github.com/leifos/tango_with_django_book

-- 
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/CAP5dYfMYFJkMmk3Zuh8jNr390ECVcfTyY_O6J-gN5NF4eXugGA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: How to store site-wide application settings

2013-06-27 Thread Felipe Coelho
2013/6/27 rush 

> Hi,
>
> Could please somebody advice how to store application settings and allow
> to edit them via admin panel?
> Settings are used site-wide, not on per user basis.
>
> This settings have different types ( text, ineteger, images aka banners )
> therefore it looks impossible to create "settings" model and store it in
> "item" - "value" fields.
>
> From the other hand it looks unreasonable to make a lot of models and put
> one or two rows into each of them.
>
> What is the best practice for that?
>
> ps. For example I have to allow configure following things: welcome text,
> 2 banners and 3 links to item from foreign application. It is easy to
> hardcode them into template, but it would be very a bit annoying to edit
> the code each time to modify something from it.
>

Hi,

I have not used those, but the djangopackages.com site has a section with a
few apps that deal with what you need:

https://www.djangopackages.com/grids/g/configuration/

-- 
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.
For more options, visit https://groups.google.com/groups/opt_out.




Re: captcha field django form designer, success html mail.

2013-06-19 Thread Felipe Coelho
2013/6/19 pradnya 

> ...
>
> ('django.forms.RegexField', _('Regex')),
>
> ('django.forms.FileField', _('File')),
>
> # ('captcha.fields.CaptchaField', _('Captcha')),
>
>
 The django-recaptcha field is called "ReCaptchaField", not just
"CaptchaField"

-- 
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.
For more options, visit https://groups.google.com/groups/opt_out.




Re: Add a file download link that work only after login

2013-06-05 Thread Felipe Coelho
2013/6/5 A T.Khan 

> I have already a Django setup running (you guessed it devstack), and it
> works fine. I have done plenty of editing already. But now I want to add a
> download button on a page, and by clicking to it, a file should be
> downloaded. Seems simple enough, but right now what I have done is adding
>
> {% trans
> "Downloadl" %}
>
>  in one of the files. This works perfectly ... however this link works but
> It is simple html encoding, and it works even when I am not logged in,
> bypassing the entire purpose of file security. I have tried to embed the
> code in some py files but then it gives me debug error.
>

Note that while Django can perform really well, serving *files* is a job
best left to the webserver itself. Take a look at django-sendfiles [1].
With it, you can use Django to test whether the user has permission to
actually download the file, and then let the webserver serve the file
transparently to the end user.

[1] https://github.com/johnsensible/django-sendfile

-- 
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




Re: Unable to access URL's query string using request.GET['next']

2013-05-24 Thread Felipe Coelho
2013/5/25 Amber Jain 

> Hello,
>
> I can't seem to access request.GET['next'] (from a URL with a query
> string). Here's what I'm doing:
> localhost:8000/dashboard/ points to dashboard() view but the dashboard()
> view uses @login_required decorator. And so, if the user isn't logged in,
> localhost:8000/dashboard/ redirects to localhost/login/?next=/dashboard/
> (but I can't access request.GET info after login).
>
> Here's my dashboard() view: http://dpaste.org/Z9dZX/ ... i.e. when
> dashboard view is loaded after successful login (from
> localhost/login/?next=/dashboard/), I see "You submitted an empty query
> string.", when in fact I expect to see the request.GET data from the URL
> query string.
>
> Can anyone point out the error(s) in my code? I can provide other parts of
> my code if you want.


I can't say exactly what you're doing *wrong*, because this is the behavior
I would expect. The ?next query parameter is used by the default login view
to tell Django where it should send the user after the user has been logged
in [1]. The ?next argument is dropped from the query string because it has
already fulfilled its purpose at that point, it wasn't an argument for your
dashboard view. Can you tell us what do you want to achieve with this?

[1]
https://github.com/django/django/blob/master/django/contrib/auth/views.py#L48

-- 
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




Re: check genre and insert

2013-04-27 Thread Felipe Coelho
2013/4/27 Hélio Miranda 

> Made the proposed change, but what happens is that when will insert the
> movie with a genre that already exists to get the id of that genre, gives
> the following error: ObjectId ('517b15658774a7177cb1280d ') as the id that
> comes from this comes mongodb form.
>
> When the genre does not exist and must enter the new genus, is not to
> enter.
>
> Does anyone know what I'm doing wrong?
>  (snip)
>

I don't know what your issue really is, but here's a quick debugging tip:
add the following lines somewhere in your view:

import code
code.interact(local=locals())

A Python interpreter will open in the same terminal where you're running
the development server and you'll be able to do anything you want, inspect
variables, query the database, you name it. Quit the interpreter (CTRL+D,
don't use exit()) and the view will finish processing normally. It should
help you finding where your assumptions are failing

-- 
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




Re: Testing with a custom user model as a ForeignKey in Django 1.5

2013-03-27 Thread Felipe Coelho
2013/3/18 Felipe Coelho 

>  1) Using tests that need the models, but don't actually touch the
>> database. This means you can put the model definition inline in the test
>> code, so AUTH_USER_MODEL has the right value when the code is run. This
>> lets us test validation conditions, etc, but not hit the database. You
>> *might* be able to get away with manually running syncdb in the middle of
>> the test -- Django's swappable_model test contains an example of doing
>> this, but only for the purposes of recreating permissions, not tables.
>
>
> The issue is that I'm trying to port an application with a whole test
> suite to work with custom user models, so I've got many tests which make
> use of classes like ModelWithForeign already.
>
>
>>  2) Define 2 "ModelWIthForeign" classes, and use the "right" one
>> depending on what the User model is at the time of test. This works fine
>> for Django's purposes because "ModelWithForeign" is a test-only model, so
>> it doesn't matter if you have a few of them. This approach won't be
>> successful if you're actually shipping ModelWithForeign as a core part of
>> your system.
>>
>>  3) Use test decorators to skip the test if a non-default user model is
>> in use.
>>
>
> The issue really is that I'm using ModelWithForeign in here, but the real
> app has one class with a ForeignKey to the user model as well. I can
> certainly skip the tests which use that model or which touch the database,
> but since this is a user registration app, being able to test it with other
> user types is quite a requirement for me.
>
>
>> Any suggestions on how to improve Django's testing infrastructure to
>> handle custom User models would be gratefully accepted (especially if they
>> come with patches). It's still new code, so reports of problems like yours
>> are helpful in shaping what features we've missed during initial
>> development.
>>
>
> I don't have any knowledge on the inside of Django, but if I figure out
> something, I'll certainly share it and try to create a patch for it.
>
> Currently I'm running the test suite using a runtests.py file like this
> one <http://stackoverflow.com/a/12260597/1202840>, and I'm thinking of
> two possible "solutions":
>
>- Making the runtests.py configure the system with different
>AUTH_USER_MODEL values each time, in a way that would run the tests
>multiple times, each time with a different database layout. The test suite
>would be in charge of creating the database each time, but since I can't
>call settings.configure multiple times, I somehow have to "unload" it
>before moving on to the next test group. This also has the disadvantage
>that, if built into my runtests.py, running the test suite when the app is
>installed in a larger project, only the tests running on the
>project-specified value for AUTH_USER_MODEL would run.
>- Maybe I could use multiple databases. In each one, the ForeignKey
>would be attached to a different user model, and then different databases
>could be selected depending on the currently defined user model. I don't
>know if that is possible, though.
>
> Hi, just a follow up. I had this on StackOverflow as well but didn't get
any answers there, so I just posted the method I'm currently using.
Essentially, I'm launching a different process for each user model, in a
way that I don't have to deal with user model changes, I just accept them
as they are and continue testing. More details:
http://stackoverflow.com/questions/15369987/testing-with-a-custom-user-model-as-a-foreignkey-in-django-1-5/15675119#15675119

-- 
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




Re: save as

2013-03-20 Thread Felipe Coelho
>
> 2013/3/20 Larry Martell 


> I'm not outputting any PDF. I have a zip file I want to download to
> the user's computer, and I also want my template rendered.


That means you have should have two requests, possibly processed by two
different views, which is what Felipe (the other one) is trying to tell you.

You must have a view that renders your template. In the resulting page,
include something to trigger the file
downloadfrom
the other view.

-- 
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




Re: Django 1.5 where are the static files of admin site

2013-03-19 Thread Felipe Coelho
2013/3/19 RedJuan 

> I'm setting up nginx to serve static files.
> Route (django / contrib / admin / media / admin) is not on the
> installation path (/ usr/local/lib/python2.7/dist-packages/django)
> Where is it located now?
>

Why don't you run "python manage.py collectstatic" and set the nginx root
for static files from there?
https://docs.djangoproject.com/en/dev/howto/static-files/#deploying-static-files-in-a-nutshell

-- 
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




Re: Testing with a custom user model as a ForeignKey in Django 1.5

2013-03-18 Thread Felipe Coelho
>
>  1) Using tests that need the models, but don't actually touch the
> database. This means you can put the model definition inline in the test
> code, so AUTH_USER_MODEL has the right value when the code is run. This
> lets us test validation conditions, etc, but not hit the database. You
> *might* be able to get away with manually running syncdb in the middle of
> the test -- Django's swappable_model test contains an example of doing
> this, but only for the purposes of recreating permissions, not tables.


The issue is that I'm trying to port an application with a whole test suite
to work with custom user models, so I've got many tests which make use of
classes like ModelWithForeign already.


>  2) Define 2 "ModelWIthForeign" classes, and use the "right" one depending
> on what the User model is at the time of test. This works fine for Django's
> purposes because "ModelWithForeign" is a test-only model, so it doesn't
> matter if you have a few of them. This approach won't be successful if
> you're actually shipping ModelWithForeign as a core part of your system.
>
>  3) Use test decorators to skip the test if a non-default user model is in
> use.
>

The issue really is that I'm using ModelWithForeign in here, but the real
app has one class with a ForeignKey to the user model as well. I can
certainly skip the tests which use that model or which touch the database,
but since this is a user registration app, being able to test it with other
user types is quite a requirement for me.


> Any suggestions on how to improve Django's testing infrastructure to
> handle custom User models would be gratefully accepted (especially if they
> come with patches). It's still new code, so reports of problems like yours
> are helpful in shaping what features we've missed during initial
> development.
>

I don't have any knowledge on the inside of Django, but if I figure out
something, I'll certainly share it and try to create a patch for it.

Currently I'm running the test suite using a runtests.py file like
this one,
and I'm thinking of two possible "solutions":

   - Making the runtests.py configure the system with different
   AUTH_USER_MODEL values each time, in a way that would run the tests
   multiple times, each time with a different database layout. The test suite
   would be in charge of creating the database each time, but since I can't
   call settings.configure multiple times, I somehow have to "unload" it
   before moving on to the next test group. This also has the disadvantage
   that, if built into my runtests.py, running the test suite when the app is
   installed in a larger project, only the tests running on the
   project-specified value for AUTH_USER_MODEL would run.
   - Maybe I could use multiple databases. In each one, the ForeignKey
   would be attached to a different user model, and then different databases
   could be selected depending on the currently defined user model. I don't
   know if that is possible, though.

-- 
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




Re: Testing with a custom user model as a ForeignKey in Django 1.5

2013-03-16 Thread Felipe Coelho
2013/3/14 Felipe Coelho 

> I'm using Django 1.5 and I'm trying to make an application work with
> any custom user model.
>
> The issue is that I want to be able to test the app as well, but I
> can't find a way to make `ForeignKey` model fields to test correctly
> using custom user models. When I run the test case attached below, I
> get this error:
>
> ValueError: Cannot assign "":
> "ModelWithForeign.user" must be a "User" instance.
>
> This is the file I'm using for testing:
>
> from django.conf import settings
> from django.contrib.auth import get_user_model
> from django.contrib.auth.tests.custom_user import CustomUser,
> CustomUserManager
> from django.db import models
> from django.test import TestCase
> from django.test.utils import override_settings
>
> class NewCustomUser(CustomUser):
> objects = CustomUserManager()
> class Meta:
> app_label = 'myapp'
>
> class ModelWithForeign(models.Model):
> user = models.ForeignKey(settings.AUTH_USER_MODEL)
>
> @override_settings(
> AUTH_USER_MODEL = 'myapp.NewCustomUser'
> )
> class MyTest(TestCase):
> user_info = {
> 'email': 'al...@bob.net',
> 'date_of_birth': '2013-03-12',
> 'password': 'password1'
> }
>
> def test_failing(self):
> u = get_user_model()(**self.user_info)
> m = ModelWithForeign(user=u)
> m.save()
>
> I'm referencing the user model in the `ForeignKey` argument list as
> described in
> https://docs.djangoproject.com/en/dev/topics/auth/customizing/#django.contrib.auth.get_user_model
> ,
> but using `get_user_model` there doesn't change anything, as the
> `user` attribute is evaluated before the setting change takes place.
> Is there a way to make this ForeignKey play nice with testing when I'm
> using custom user models?
>

Since this question didn't get as much love as I expected, let me rephrase
it:

Is is possible to drop and recreate specific database tables during
testing? That would allow me to alter the model with the offending
ForeignKey, recreate the table and use it during testing. The database
content is already flushed between tests, so if I group the tests in a good
way I hope that the overhead of a drop/create table will not be as heavy as
it would be.

-- 
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




Testing with a custom user model as a ForeignKey in Django 1.5

2013-03-13 Thread Felipe Coelho
I'm using Django 1.5 and I'm trying to make an application work with
any custom user model.

The issue is that I want to be able to test the app as well, but I
can't find a way to make `ForeignKey` model fields to test correctly
using custom user models. When I run the test case attached below, I
get this error:

ValueError: Cannot assign "":
"ModelWithForeign.user" must be a "User" instance.

This is the file I'm using for testing:

from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.tests.custom_user import CustomUser,
CustomUserManager
from django.db import models
from django.test import TestCase
from django.test.utils import override_settings

class NewCustomUser(CustomUser):
objects = CustomUserManager()
class Meta:
app_label = 'myapp'

class ModelWithForeign(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)

@override_settings(
AUTH_USER_MODEL = 'myapp.NewCustomUser'
)
class MyTest(TestCase):
user_info = {
'email': 'al...@bob.net',
'date_of_birth': '2013-03-12',
'password': 'password1'
}

def test_failing(self):
u = get_user_model()(**self.user_info)
m = ModelWithForeign(user=u)
m.save()

I'm referencing the user model in the `ForeignKey` argument list as
described in 
https://docs.djangoproject.com/en/dev/topics/auth/customizing/#django.contrib.auth.get_user_model,
but using `get_user_model` there doesn't change anything, as the
`user` attribute is evaluated before the setting change takes place.
Is there a way to make this ForeignKey play nice with testing when I'm
using custom user models?

-- 
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.