Re: I'm missing something with Models and database migrations

2015-12-27 Thread Peter of the Norse
OK.  Let’s take a look at the documentation 
.
  Here is what you are looking for:

class Invoice(models.Model):
invoice_date = models.DateField(‘created date’, auto_now=True)
line_item_set = models.ManyToManyField(LineItem, through=‘InvoiceLineItem’)

class LineItem(models.Model):
descr = models.CharField(max_length=100)
cost = models.DecimalField(max_digits=5, decimal_places=2)

class InvoiceLineItem(models.Model):
created_dt = models.DateField(auto_now=True)
invoice = models.ForeignKey(Invoice)
line_item = models.ForeignKey(LineItem)

When you use through with a ManyToManyField, it doesn’t create an extra table; 
otherwise, it does.  Also, I’ve cleaned up things so that they match Django 
standards.


> On Dec 22, 2015, at 10:33 PM, Michael Molloy  wrote:
> 
> I figured it out, again with help from Vijay. Thanks for getting me thinking 
> in the right direction.
> 
> So here is my cross reference model:
> 
> class Invoice_Line_Items(models.Model):
>  created_dt = models.DateField('created date', auto_now=True)
>  invoice = models.ManyToManyField(Invoices)
>  line_item = models.ManyToManyField(Line_Items)
> 
> 
> 
> I was incorrectly looking for a single table named Invoice_Line_Items because 
> I didn't understand how Django creates multiple tables to deal with 
> ManyToMany relationships like this. The table I was expecting was there, but 
> it didn't have columns for invoice IDs or line_item IDs. Django actually 
> created these three tables to handle this:
> 
> gpga_invoice_line_items
> gpga_invoice_line_items_invoice
> 
> gpga_invoice_line_items_line_item  
> 
> 
> Thanks again.
> --M
> 
> 
> 
> On Tuesday, December 22, 2015 at 8:49:10 PM UTC-6, Michael Molloy wrote:
> Still confused. I think I still need to create the cross reference Django 
> model myself, and then use makemigrations to turn it into a table.
> 
> I need a place to store invoices and associated line items, so if the model 
> code I put in my first post is incorrect, what should it look like? 
> 
> --Michael
> 
> On Tuesday, December 22, 2015 at 8:29:36 PM UTC-6, Michael Molloy wrote:
> I was trying to create the cross reference table myself. I'm coming at this 
> from a java/oracle background where I would create two tables (invoices and 
> line_items), and then a third (invoice_line_item_xref) that would contain 
> invoice IDs and associated line item IDs. I think you just helped me see my 
> misunderstanding of how Django does things.
> 
> So, in the list of tables (now that I know what I'm looking for), I see a 
> table called invoice_line_items_xref_invoice_id and another called 
> invoice_line_items_xref_line_item_id. Those must be the two tables that 
> Django created to handle the many to many relationship. Is that correct? 
> 
> --Michael 
> 
> On Tuesday, December 22, 2015 at 6:29:51 PM UTC-6, Vijay Khemlani wrote:
> You have two pairs of fields with the same name (line_item_id and 
> invoice_id), what are you trying to do exactly? Why the IntegerFields?
> 
> In a Many To Many relation the columns are added to a third table between the 
> models.
> 
> On Tue, Dec 22, 2015 at 9:20 PM, Michael Molloy > wrote:
> Python 3.3 and Django 1.8 running on Openshift with a Postgresql database
> 
> I'm trying to set up an Invoices table, a Line_Item table, and a cross 
> reference between them. Here are the relevant models:
> 
> 
> class Invoices(models.Model):
>  invoice_date = models.DateField('created date', auto_now=True)
> 
> class Line_Items(models.Model):
>  descr = models.CharField(max_length=100)
>  cost = models.DecimalField(max_digits=5, decimal_places=2)
> 
> class Invoice_Line_Items_Xref(models.Model):
>  created_dt = models.DateField(auto_now=True)
>  invoice_id = models.IntegerField(default=0)
>  line_item_id = models.IntegerField(default=0)
>  invoice_id = models.ManyToManyField(Invoices)
>  line_item_id = models.ManyToManyField(Line_Items)
> 
> 
> 
> I don't think the syntax for the cross reference table above is correct, but 
> it is one of the permutations that I've tried. The layout above resulted in 
> this migration after running makemigrations
> 
> operations = [
>  migrations.AddField(
>  model_name='invoice_line_items_xref',
>  name='invoice_id',
>  field=models.ManyToManyField(to='gpga.Invoices'),
>  ),
>  migrations.AddField(
>  model_name='invoice_line_items_xref',
>  name='line_item_id',
>  field=models.ManyToManyField(to='gpga.Line_Items'),
>  ),
> ]
> 
> 
> 
> However, when I push the code to Openshift, even though the migration runs 
> against the database, the invoice_id and line_item_id columns do not appear 
> on the database table. 
> 
> I have no idea what I'm doing wrong. Thank you for any help.
> 
> --Michael
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To 

Display form errors above its respective field with ajax

2015-12-27 Thread Robin Lery
I have a contact form through which users would be able to contact me. I am
using django with ajax, and it works fine if there's no error. I would like
to show the errors if there's any like it displays above the input fields
and not just the errors, but both the input and the errors. It does however
differentiate between the success and error result, as the ajax request was
successful. But I need to display the actual form errors.

I would like to display the errors like the below as it normally does
without ajax:


​

How do I that? Your help will be very much appreciated. Thank you.

views:

def contact(request):
if request.is_ajax() and request.POST:
form = ContactForm(request.POST)
if form.is_valid():
new_contact = form.save()
data = {
'result': 'success',
'message': 'Message Sent.'
}
return JsonResponse(data)
else:
data = {
'result': 'error',
'message': 'Form invalid',
'form': 'oops.'
}
return JsonResponse(data)
else:
form = ContactForm()
return render(request, 'public_contact.html', {
'form': form
})

js:

contact_right_form.find('#submit').on('click', function(event) {
event.preventDefault();
$.ajax({
type: contact_right_form.attr('method'),
url: '/contact/',
data: contact_right_form.serialize(),
dataType: 'json',
success: function(data) {
if ( data.result == 'success') {
contact_right_message_sent.text(data.message);
contact_right_message_sent.show();
}
else {
contact_right_message_sent.text(data.message);
contact_right_message_sent.show();
}
},
error: function() {
contact_right_message_sent.text('Sorry! Something went wrong.')
}
});
})

-- 
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/CA%2B4-nGrxLKo%3DnGdgvjgGDVkcK9K4UW-8WhJbNDLQ1kVPCbvHeg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: settings.py variables in fixtures

2015-12-27 Thread Russell Keith-Magee
Hi Salvatore,

On Mon, Dec 28, 2015 at 12:09 AM, Salvatore Scaramuzzino <
salvatorescaramuzz...@gmail.com> wrote:

> hi to everyone
>
> i'm newbye in django, but i really appreciate this useful framework. I
> have a simple question but i don't know if is appropriate.
>
> i have a fixture written in JSON and i use it to provide initial data to
> my webapp. i want to provide in this fixture absolute paths in order to
> store some images. is there a way to use in JSON fixtures (or in
> another format) variables defined in settings.py (eg. base path of the app)?
>

To make sure I’ve understood your use case: you want to have a JSON (or
similar format) file that contains absolute paths to some images, and load
these images into your database, relative to some setting in your
settings.py file.

If that’s the case, you probably won’t be able to use Django’s default
serializer framework - Django’s serialisers are designed to be little more
than a dump (and restore) of literal database content. Transforming input
along the way isn’t something they are designed to do. You *might* be able
to do something by writing a custom field and manipulating the
deserialisation code on the field, but honestly, it’s going to be more
trouble than it’s worth.

What you are trying to do is still completely achievable, though - you just
have to write your own management command. That command will take the name
of the JSON file as an argument, parse it and iterate through the content,
and process the absolute paths into whatever local storage path you want to
use.

I hope that makes sense; if it doesn’t (or I’ve misunderstood your use
case) let me know and I’ll elaborate some more.

Yours,
Russ Magee %-)

-- 
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/CAJxq848ENYcM4LwdgBw9DWTb8ta7NJp6Pk32z7EuzQg%2BcajBtA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: testing django migrations

2015-12-27 Thread Gergely Polonkai
I'm also interested in that, except that I'd like to test RunPython steps.
On Dec 28, 2015 12:32 AM, "Dan Tagg"  wrote:

> Hi,
>
> I used this:
> https://github.com/plumdog/django_migration_testcase
>
> It's only worth writing tests if you are modifying code created by
> makemigrations.
>
> Dan
>
> On 27 December 2015 at 10:14, varun naganathan <
> varunnaganathan...@gmail.com> wrote:
>
>> Hi,I wanted to know how am i supposed to test the working of django
>> migrations.As in how the migrations generate the SQL query.Basically I want
>> to inspect how the sql query generated as in the output of "./manage.py
>> sqlmigrate" is being generated.I've generally used "pdb" along wth
>> unittest.TestCase,but for migrations I'm pretty much stuck.Does anyone
>> probably have a sample test they wrote to test the django migrations.
>> Thanks in advance.
>>
>> --
>> 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/b6364f7e-56ab-4682-ad0a-3684e6199c8f%40googlegroups.com
>> 
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> --
> Wildman and Herring Limited, Registered Office: 52 Great Eastern Street,
> London, EC2A 3EP, Company no: 05766374
>
> --
> 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/CAPZHCY5CJ_9jex%2BnKNi%3DCy2XG9X9zrLS%3DJBW-mUmDoCpK6%2BTPA%40mail.gmail.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/CACczBUL7z8M8nPbsAO2jM2EvyMCqxT4JJVGGSvA8MW__a8E6VQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: testing django migrations

2015-12-27 Thread Dan Tagg
Hi,

I used this:
https://github.com/plumdog/django_migration_testcase

It's only worth writing tests if you are modifying code created by
makemigrations.

Dan

On 27 December 2015 at 10:14, varun naganathan  wrote:

> Hi,I wanted to know how am i supposed to test the working of django
> migrations.As in how the migrations generate the SQL query.Basically I want
> to inspect how the sql query generated as in the output of "./manage.py
> sqlmigrate" is being generated.I've generally used "pdb" along wth
> unittest.TestCase,but for migrations I'm pretty much stuck.Does anyone
> probably have a sample test they wrote to test the django migrations.
> Thanks in advance.
>
> --
> 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/b6364f7e-56ab-4682-ad0a-3684e6199c8f%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Wildman and Herring Limited, Registered Office: 52 Great Eastern Street,
London, EC2A 3EP, Company no: 05766374

-- 
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/CAPZHCY5CJ_9jex%2BnKNi%3DCy2XG9X9zrLS%3DJBW-mUmDoCpK6%2BTPA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: django 1.9 - application does not

2015-12-27 Thread Sergiy Khohlov
Django toolbar does not work with 1.9 please comment it in and restart
server
24 груд. 2015 19:36 "Shekar Tippur"  пише:

> Just for testing, I downgraded django to 1.8.4
>
> # pip-3.4 install Django==1.8.4
>
> You are using pip version 6.1.1, however version 7.1.2 is available.
>
> You should consider upgrading via the 'pip install --upgrade pip' command.
>
> Collecting Django==1.8.4
>
>   Downloading Django-1.8.4-py2.py3-none-any.whl (6.2MB)
>
> 100% || 6.2MB 89kB/s
>
> Installing collected packages: Django
>
>   Found existing installation: Django 1.9
>
> Uninstalling Django-1.9:
>
>   Successfully uninstalled Django-1.9
>
> Successfully installed Django-1.8.4
>
>
> Now I get ImportError: No module named 'provider'.
>
>
> - Shekar
>
> --
> 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/c6e8eebf-a65e-4161-be30-215dd926963c%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/CADTRxJOnNh41Vtqzr07VK%2BPt%3D_SLxOUTe%3DU504gtH6tC8_zzHQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Using API's efficiently in the framework

2015-12-27 Thread Andrew Farrell
It looks like you several words.

To pre-empt a question you might be asking, if you are trying to call out
to an API during the request-response cycle, you can find that the request
to your app times out. To solve this, people often use a task-runner such
as celery (https://github.com/celery/celery). Check out this tutorial:
http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html

On Sunday, December 27, 2015, Alistair Lobo 
wrote:

> I'm a beginner to using API's
>
> --
> 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/5cea17b1-fd12-4808-a13e-ccd9f4abbd6a%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/CA%2By5TLbR%3DPpF%2BXskQtypBTjudSuX67CsV8XTG8TUAq2Kc_539g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Using API's efficiently in the framework

2015-12-27 Thread Alistair Lobo
I'm a beginner to using API's

-- 
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/5cea17b1-fd12-4808-a13e-ccd9f4abbd6a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


settings.py variables in fixtures

2015-12-27 Thread Salvatore Scaramuzzino
hi to everyone

i'm newbye in django, but i really appreciate this useful framework. I have 
a simple question but i don't know if is appropriate.

i have a fixture written in JSON and i use it to provide initial data to my 
webapp. i want to provide in this fixture absolute paths in order to store 
some images. is there a way to use in JSON fixtures (or in another 
format) variables defined in settings.py (eg. base path of the app)?

thanks in advance

-- 
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/e10405bd-994b-41db-b076-d028206ca25d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


React.js with django-rest-framework using SessionAuthentication: How to include CSRF-Token documentation has not helped me :/

2015-12-27 Thread lnzy35en
 


Hi,

I am getting crazy finding out how to submit data to the django-rest-api via react.js to include a valid CSRF token by using SessionAuthentication.

My template is very basic:

{% extends "base.html" %}

{% load staticfiles %}

{% block title %} ToDo List {% endblock %}

{% block content %}

    

{% endblock %}

The corresponding JSX File (which I then transform via jsx –x jsx . . to a valid .js file) contains the following:

 

var ToDoForm = React.createClass({

    getInitialState: function() {

   return {

   "todo_source": "",

   "todo_description": "",

   "todo_due_date": '2015-12-21',

   "todo_prio": "1"

   };

    },

    handleSourceChange: function(e) {

   this.setState({todo_source: e.target.value});

    },

    handleDescriptionChange: function(e) {

   this.setState({todo_description: e.target.value});

    },

    handleDateChange: function(e) {

   this.setState({todo_due_date: e.target.value});

    },

   

    handlePrioChange: function(e) {

   this.setState({todo_prio: e.target.value});

    },

       

    handleSubmit: function(e) {

   e.preventDefault();

  

   $.ajax({

   url: this.props.url,

   dataType: 'json',

   type: 'POST',

   data: this.props,

   success: function(data) {

   this.setState({data: data});

   }.bind(this),

   error: function(xhr, status, err) {

   console.error(this.props.url, status, err.toString());

   }.bind(this)

   });

    },

 

    render: function() {

   return (

   

   

  

   

   

   

   

  

        

   

   

   

  

   

   

   

   

   

   

   

   

  

   Submit

   

   

   );

    }

});

ReactDOM.render(

 

So can anybody please tell me how to include the csrf-token in react.js? https://docs.djangoproject.com/en/1.9/ref/csrf/ has not really helped me nor did I find any example to accomplish that via google.

 

My also data: this.props is wrong then? Since I have to include the token?

Is there a better way?

 

Thank you very much!

Best Regards




-- 
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/trinity-89d49cc4-75fa-4819-b907-b52935f8e3c7-1451219372016%403capp-gmx-bs17.
For more options, visit https://groups.google.com/d/optout.


testing django migrations

2015-12-27 Thread varun naganathan
Hi,I wanted to know how am i supposed to test the working of django 
migrations.As in how the migrations generate the SQL query.Basically I want 
to inspect how the sql query generated as in the output of "./manage.py 
sqlmigrate" is being generated.I've generally used "pdb" along wth 
unittest.TestCase,but for migrations I'm pretty much stuck.Does anyone 
probably have a sample test they wrote to test the django migrations.
Thanks in advance.

-- 
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/b6364f7e-56ab-4682-ad0a-3684e6199c8f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Running unittests on different databases

2015-12-27 Thread varun naganathan
Thanks a lot.That really helped. 

-- 
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/e04929e0-1c07-4e6c-bae0-8079bdd47973%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.