Tuesday, April 1, 2014

Re: ManyToMany quick question


Camillo, YOU'RE DA MAN! Thank you so much? One day, at djangocon, I will buy you a beer

--
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+unsubscribe@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/a06f1d19-2c50-4c50-b8f4-6f3ff9643b8b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: ManyToMany quick question

Hello,

By using default table names and default (or automatic) many to many field management, you get a join table like countries_products, but in your case, you are doing the many to many field management through the Imports model, so in the DB you should see data in the imports table for each relation between Countries and Products.

When doing many to many in a relational model, as in a RDBMS or SQL database, that relation must be modeled by using a third table. This is in a normalized schema. So, you will not see a join field, but a join table. Note: in a non-normalized schema it is possible to have a field for a many to many relation, but with data duplication, not the case of the Django ORM.

So in your model you will never see a country_name field. Think of this: if the country table had a country_name field, you can only put one value in that field for every row, so only one value for any Django model in the ORM, and this is not many to many, but one to many.

In your case, the relation is in the Imports model and hence in the imports database table.

Regards,
Camilo

On Tuesday, April 1, 2014 5:51:18 AM UTC-4:30, willyhakim wrote:
Below is my models.py
My problem is when I syncdb (I have dropped the db couple times to start over)
the country_name field with m2m never shows in the db. what am I doing wrong? I am using postgres

class Products(models.Model):

    hs_number = models.CharField(primary_key=True, blank=True, max_length=4)
    product_descript = models.CharField(max_length=250)

    class Meta:
        db_table = "Products"

    def __unicode__(self):
        return self.hs_number


class Countries(models.Model):
    country_id = models.CharField(primary_key=True, max_length=2, blank=True)
    country_name = models.ManyToManyField(Products, through="Imports", blank=True)
    region = models.CharField(max_length=200)

class Imports(models.Model):
    hs_number = models.ForeignKey(Products)
    country_id = models.ForeignKey(Countries)
    imported_value2008 = models.DecimalField(max_digits=12, decimal_places=2, default=0.00)
    imported_value2009 = models.DecimalField(max_digits=12, decimal_places=2, default=0.00)

--
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+unsubscribe@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/0fb3823a-414e-4255-85f8-48acfa21878a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: Filtersets

Hi Conrad,
I've built a django app to build queries like this. The one that is available on github right now is somewhat specific to my initial use case, but I plan on releasing a new, much more modular version of it this week. If you want to take a look at it and use it, or wait for the update, or just take ideas from it, here it is:

https://github.com/ckirby/django-modelqueryform

~C. Kirby

On Tuesday, April 1, 2014 6:16:05 AM UTC-5, Conrad Rowlands wrote:
Hi All. I wonder if anyone can point me in the right direction. I am building a web api using django and FilterSets and wish to allow consumers to perform a query along the lines of

Manufacturer =x OR manufacturer = y etc etc

Is this achievable out of the box and how would you specify this within the url? My current url looks like this

http://127.0.0.1:8000/compare/api/instance/?Manufacturer=x&Manufacturer=y

but this only performs an AND on the underlying data

Manufacturer =x AND manufacturer = y etc etc

Also, please note that whilst I would like to perform an OR with regard to the manufacturer if I include other fields to be queried I would expect these to be AND'd for example

http://127.0.0.1:8000/compare/api/instance/?Manufacturer=x&Manufacturer=yModel=z

should equate to

(Manufacturer =x OR manufacturer = y) AND Model = z

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+unsubscribe@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/0e36096f-9927-4ab9-96d7-e66dc5e1c77a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: Why can invalid models be saved?

Most validation is in the processing of HTML forms, to help the site user (or admin) avoid errors.  Checking on the way to the database is much more limited, and is usually limited to the ability to coerce the field to the correct type.  Since django works with many databases, there is very little common ground for specifying that the database itself check, maybe just not null.

A programmer, using the models directly (that's the presumption if you're typing python statements) is expected to know what he's doing.

Bill


On Tue, Apr 1, 2014 at 4:19 AM, Claus Conrad <lists@clausconrad.com> wrote:
Hi,

sorry for the newbie question!

In the admin I cannot create a user without a username, but on the shell it is possible:

>>> from django.contrib.auth.models import User
>>> u = User()
>>> u.save()
>>> u.id
2

I am trying to understand why this is possible. Does this mean model constraints in general are not enforced when instances are created and saved? Thus I will always have to validate my models before saving them, unless I set null=True on a field, in which case I'd expect to get an exception?

Thanks,
Claus

--
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+unsubscribe@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/4b097bd9-cc8b-4511-8efa-2fb4537b702f%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+unsubscribe@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/CAB%2BAj0tMPgCfa4vk5Fib19tanNjMrGV88yuS61%2B3gp0kpykY-A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Re: ManyToMany quick question



I think I was missing the concept all together. So the m2m field is just there to show the relationship but does not take any data. That is why when I look in pgadmin, I do not see it. 
So sorry, I was missing the concept all together.

--
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+unsubscribe@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/4b814fc7-4ca0-4249-8226-536a3381f951%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: ManyToMany quick question

I don't know why you're calling a ManyToManyField to Products a country name, but you shouldn't get a country_name field - the Imports table will contain all the data needed to associate countries with products.

What exactly do you want to be stored in the country_name field on the Countries model? If you want it to be the name of the country, it should be a separate field from the ManyToManyField altogether.

On Tuesday, April 1, 2014 10:21:18 AM UTC, willyhakim wrote:
Below is my models.py
My problem is when I syncdb (I have dropped the db couple times to start over)
the country_name field with m2m never shows in the db. what am I doing wrong? I am using postgres

class Products(models.Model):

    hs_number = models.CharField(primary_key=True, blank=True, max_length=4)
    product_descript = models.CharField(max_length=250)

    class Meta:
        db_table = "Products"

    def __unicode__(self):
        return self.hs_number


class Countries(models.Model):
    country_id = models.CharField(primary_key=True, max_length=2, blank=True)
    country_name = models.ManyToManyField(Products, through="Imports", blank=True)
    region = models.CharField(max_length=200)

class Imports(models.Model):
    hs_number = models.ForeignKey(Products)
    country_id = models.ForeignKey(Countries)
    imported_value2008 = models.DecimalField(max_digits=12, decimal_places=2, default=0.00)
    imported_value2009 = models.DecimalField(max_digits=12, decimal_places=2, default=0.00)

--
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+unsubscribe@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/0cb18bdf-fbbb-46e6-9fdf-43cf6331a79d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: Use custom HTML to render forms

On 1/04/2014 8:12 PM, Daniel Roseman wrote:
> On Tuesday, 1 April 2014 00:40:44 UTC+1, Fred DJar wrote:
>
> HELP PLEASE

I agree with DR but it might be interesting to know what your problem
might be ...

>
>
> No.
>
> You don't get to demand that people help you. If someone knows the
> answer to your question, and they have time to answer - and if they can
> work out what you're asking, which isn't obvious - they will reply. But
> if you want help on demand, you need to pay someone.
> --
> DR.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users+unsubscribe@googlegroups.com
> <mailto:django-users+unsubscribe@googlegroups.com>.
> To post to this group, send email to django-users@googlegroups.com
> <mailto: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/162b8220-9c99-4f7f-ad36-7701d6a40451%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/162b8220-9c99-4f7f-ad36-7701d6a40451%40googlegroups.com?utm_medium=email&utm_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+unsubscribe@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/533AA81F.3070205%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.