Need help with models.ManyToManyField() as it always includes all objects when I click "save and continue editing"

2019-05-14 Thread Onur Seker
Hi,
everytime I am selecting only one object from my Product from all my 
Products available in my Cart. 
It saves and includes all Products to the model.ManyToManyField(Product) in 
my Cart model.

I dont know what is wrong with my code here.

Can somebody help me please?

-- 
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/85bc5746-d2c5-4d97-bf99-2baa07e2da2d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: models.ManyToManyField

2013-04-18 Thread Gabriel
The first solution works like this:
If you create a foreign key from ModelA to ModelB, Django automatically
create something like B.modela_set that works just like the objects
attribute, but just for the A objects that are related to B, right?
That's why you can du stuff like attore.film_set.all() to get all the films
the actor was in. Django creates film_set for you in Attore instances
because you have a ForeignKey from Film to Attore

So, if you wanna list all the films for each actor, something like

actors_dict = dict()
actors = Attore.objects.all()
for actor in actors:
  full_name = '{} {}'.format(actor.nome, actor.cognome)
  actors_list[actor.cognome] = [film.titolo for film in
actor.film_set.all()]

Then actors_list would be a dictionary like:
{
  'Nicholas Cage': ['Gone in 60 Seconds', 'Face Off'],
  'Jack Nicholson': ['Batman', 'The Shining']
}


There are different ways of doing this, and I get the feeling you'd like
something with templates. But this is pretty much the gist of it.

- Gabe


On Thu, Apr 18, 2013 at 5:44 AM, Federico Erbea  wrote:

> I think it is the first solution that you have proposed, but I can't
> figure out how to use it. Also, is there a way to make sure that the ID is
> automatically taken? With a proper publisher for loop I would like all the
> lists of movies for each actor.
>
> This is my urls.py
>
> from django.conf.urls.defaults import *
>
> urlpatterns = patterns('',
> (r'^Film$', 'Database.views.film'),
> )
>
> and this is my views.py
>
> from django.shortcuts import render_to_response
> from django.template import RequestContext
> from models import *
>
> def film(request):
> film = Film.objects.all()
> return render_to_response('Film.html'**, { 'film': film, })
>
> Thanks for the help.
>
> --
> 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.
>
>
>

-- 
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: models.ManyToManyField

2013-04-18 Thread Federico Erbea
I think it is the first solution that you have proposed, but I can't figure 
out how to use it. Also, is there a way to make sure that the ID is 
automatically taken? With a proper publisher for loop I would like all the 
lists of movies for each actor.

This is my urls.py

from django.conf.urls.defaults import *

urlpatterns = patterns('',
(r'^Film$', 'Database.views.film'),
)

and this is my views.py

from django.shortcuts import render_to_response
from django.template import RequestContext
from models import *

def film(request):
film = Film.objects.all()
return render_to_response('Film.html', { 'film': film, })

Thanks for the help.

-- 
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: models.ManyToManyField

2013-04-18 Thread Federico Erbea
I think it is the first solution that you have proposed, but I can't figure 
out how to use it. Also, is there a way to make sure that the ID is 
automatically taken? With a proper publisher for loop I would like all the 
lists of movies for each actor.

This is my urls.py

from django.conf.urls.defaults import *

urlpatterns = patterns('',
(r'^Film$', 'Database.views.film'),
)

and this is my views.py

from django.shortcuts import render_to_response
from django.template import RequestContext
from models import *

def film(request):
film = Film.objects.all()
return render_to_response('Film.html', { 'film': film, 'regista': 
regista, })

Thanks for the help.

-- 
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: models.ManyToManyField

2013-04-17 Thread Gabriel
 Hey,
I didn't quite understand what you're trying to do, so I'm covering both
interpretations.

1- If you're trying to, for instance, get all the films an actor was in you
can, as explained here
https://docs.djangoproject.com/en/1.5/topics/db/queries/#many-to-many-relationships(the
link is for Django 1.5, but don't worry if you're using an older
version), use something like:

attore = Attore.objects.get(pk=1)  # Get an actor from the database
attore.film_set.all()  # Get the list of all the films the actor been in

2- If you're trying to filter Films based on actor information, you can do
something like this:

Film.objects.filter(attori__cognome='Travolta')  # Get all films that have
at least one actor with the last name Travolta


Now for something completely different, I don't know what are your
requisites, but do you really need classes (and then database tables) for
stuff like movie duration and release year? Seems like overkill and can end
up hurting performance.
Usually (not always) if you have a class with just one attribute, you don't
actually need a class.

I hope one of these was what you're looking for.

- Gabe

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




models.ManyToManyField

2013-04-17 Thread Federico Erbea
Hello everyone, I am a neophyte and maybe it's a stupid question so thank 
you in advance aid.
My question is: can I go back and retrieve all the movies assigned to a 
specific actor, director, or other through the class film? Or do I have to 
create a specific class for each item to which I want to connect the Film? 
(as I have done precisely with the Film, the fact is that I looked very 
hard-working and maybe there's a better solution)
Here's the models.py:

from django.db import models

class Umore( models.Model ):
umore = models.CharField( max_length=25 )
def __unicode__(self):
return self.umore
class Meta:
verbose_name_plural = "Umori"

class Genere_Film( models.Model ):
genere = models.CharField( max_length=25 )
def __unicode__(self):
return self.genere
class Meta:
verbose_name_plural = "Generi_Film"

class Anno( models.Model ):
anno = models.CharField( max_length=4 )
def __unicode__(self):
return self.anno
class Meta:
verbose_name_plural = "Anno"

class Durata( models.Model ):
durata = models.CharField( max_length=5 )
def __unicode__(self):
return  self.durata
class Meta:
verbose_name_plural = "Durata"

class Attore( models.Model ):
nome = models.CharField( max_length=30 )
cognome = models.CharField( max_length=30 )
foto = models.CharField( max_length=100 )
data_inserimento = models.DateField( null=True, verbose_name="data 
d'inserimento" )
def __unicode__(self):
return self.nome + " " + self.cognome + " " + self.foto
class Meta:
verbose_name_plural = "Attori"

class Regista( models.Model ):
nome = models.CharField( max_length=30 )
cognome = models.CharField( max_length=30 )
foto = models.CharField( max_length=100 )
data_inserimento = models.DateField( null=True, verbose_name="data 
d'inserimento" )
def __unicode__(self):
return self.nome + " " + self.cognome + " " + self.foto
class Meta:
verbose_name_plural = "Registi"

class Studio( models.Model ):
nome = models.CharField( max_length=30 )
foto = models.CharField( max_length=100 )
data_inserimento = models.DateField( null=True, verbose_name="data 
d'inserimento" )
def __unicode__(self):
return self.nome #+ " " + self.foto
class Meta:
verbose_name_plural = "Studi"

class Trailer( models.Model ):
   trailer = models.CharField( max_length=100 )
   data_inserimento = models.DateField( null=True, verbose_name="data 
d'inserimento" )
   def __unicode__(self):
return self.trailer
class Meta:
verbose_name_plural = "Trailers"

class Film( models.Model ):
titolo = models.CharField( max_length=39 )
trama = models.CharField( max_length=1000 )
locandina = models.CharField( max_length=100 )
copertina = models.CharField( max_length=100 )
trailer = models.ForeignKey( Trailer )
data_inserimento = models.DateField( null=True, verbose_name="data 
d'inserimento" )
anno = models.ForeignKey( Anno )
durata = models.ForeignKey( Durata )
attori = models.ManyToManyField( Attore )
registi = models.ManyToManyField( Regista )
studi = models.ManyToManyField( Studio )
umori = models.ManyToManyField( Umore )
generi = models.ManyToManyField( Genere_Film )
def __unicode__(self):
return self.titolo + " " + self.trama + " " + self.locandina + " " 
+ self.copertina
class Meta:
verbose_name_plural = "Film"

-- 
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: models.ManyToManyField subclass, problem with getting values by form.

2009-06-28 Thread Vladimir Shulyak

Well... Problem closed. I was trying to override things that I
shouldn't. Now I got the idea of returning manager by __get__ and
everything is just fine :)

On Jun 27, 11:13 pm, Vladimir Shulyak <nc0...@gmail.com> wrote:
> Hello,
>
> I am trying to develop sublcass of models.ManyToManyField. This class
> should return text representations of objects to forms.TextField (like
> TagField from django-tagging). The reason to make my own
> representation is because:
> - I don't need content types
> - I need "front face" to ManyToManyField, but TagField uses CharField
> and stores duplicate data.
>
> So I come to implementing my own descriptor and field like this
> (minimal code just to show exception, dpaste of same code for syntax
> highlightinghttp://dpaste.com/60470/):
>
> #descriptor of field
> class MultiModelSubclassDescriptor
> (ReverseManyRelatedObjectsDescriptor):
>
>     def __init__(self, m2m_field):
>         super(MultiModelSubclassDescriptor, self).__init__(m2m_field)
>
>     def __get__(self, instance, instance_type=None):
>         from products.models import Ingridient
>         return ", ".join(["%s" % ing.name for ing in
> Ingridient.objects.all()])
>         #super(MultiModelSubclassDescriptor, self).__get__(instance,
> instance_type)
>
>     def __set__(self, instance, value):
>         pass
>         #super(MultiModelSubclassDescriptor, self).__set__(instance,
> value)
>
> #field
> class MultiModelSubclassField(ManyToManyField):
>
>     def __init__(self, to, **kwargs):
>         super(MultiModelSubclassField, self).__init__(to, **kwargs)
>
>     def contribute_to_class(self, cls, name):
>         super(MultiModelSubclassField, self).contribute_to_class(cls,
> name)
>
>         # Make this object the descriptor for field access.
>         setattr(cls, self.name, MultiModelSubclassDescriptor(self))
>
>     def value_from_object(self, obj):
>         "Returns the value of this field in the given model instance."
>         return getattr(obj, self.attname) #original getattr(obj,
> self.attname).all() for queryset
>
>     def formfield(self, **kwargs):
>         f = FormCharField(widget = TextInput())
>         return f
>
> But as my field is a subclass of ManyToManyField, django tries to get
> PKs of my object, not value.
> Piece of django code which is responsible for it:
>
>     for f in opts.fields + opts.many_to_many:
>         if not f.editable:
>             continue
>         if fields and not f.name in fields:
>             continue
>         if exclude and f.name in exclude:
>             continue
>         if isinstance(f, ManyToManyField):
>             # If the object doesn't have a primry key yet, just use an
> empty
>             # list for its m2m fields. Calling f.value_from_object
> will raise
>             # an exception.
>             if instance.pk is None:
>                 data[f.name] = []
>             else:
>                 # MultipleChoiceWidget needs a list of pks, not object
> instances.
>                 data[f.name] = [obj.pk for obj in f.value_from_object
> (instance)] """ HERE I get exception """
>
>         else:
>             data[f.name] = f.value_from_object(instance)
>
> There is a hack I to overcome this I beleive...
>
> Any help much appreciated, I am fighting with it for 4 days...
> 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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



models.ManyToManyField subclass, problem with getting values by form.

2009-06-27 Thread Vladimir Shulyak

Hello,

I am trying to develop sublcass of models.ManyToManyField. This class
should return text representations of objects to forms.TextField (like
TagField from django-tagging). The reason to make my own
representation is because:
- I don't need content types
- I need "front face" to ManyToManyField, but TagField uses CharField
and stores duplicate data.

So I come to implementing my own descriptor and field like this
(minimal code just to show exception, dpaste of same code for syntax
highlighting http://dpaste.com/60470/):

#descriptor of field
class MultiModelSubclassDescriptor
(ReverseManyRelatedObjectsDescriptor):

def __init__(self, m2m_field):
super(MultiModelSubclassDescriptor, self).__init__(m2m_field)

def __get__(self, instance, instance_type=None):
from products.models import Ingridient
return ", ".join(["%s" % ing.name for ing in
Ingridient.objects.all()])
#super(MultiModelSubclassDescriptor, self).__get__(instance,
instance_type)

def __set__(self, instance, value):
pass
#super(MultiModelSubclassDescriptor, self).__set__(instance,
value)

#field
class MultiModelSubclassField(ManyToManyField):

def __init__(self, to, **kwargs):
super(MultiModelSubclassField, self).__init__(to, **kwargs)

def contribute_to_class(self, cls, name):
super(MultiModelSubclassField, self).contribute_to_class(cls,
name)

# Make this object the descriptor for field access.
setattr(cls, self.name, MultiModelSubclassDescriptor(self))

def value_from_object(self, obj):
"Returns the value of this field in the given model instance."
return getattr(obj, self.attname) #original getattr(obj,
self.attname).all() for queryset

def formfield(self, **kwargs):
f = FormCharField(widget = TextInput())
return f





But as my field is a subclass of ManyToManyField, django tries to get
PKs of my object, not value.
Piece of django code which is responsible for it:

for f in opts.fields + opts.many_to_many:
if not f.editable:
continue
if fields and not f.name in fields:
continue
if exclude and f.name in exclude:
continue
if isinstance(f, ManyToManyField):
# If the object doesn't have a primry key yet, just use an
empty
# list for its m2m fields. Calling f.value_from_object
will raise
# an exception.
if instance.pk is None:
data[f.name] = []
else:
# MultipleChoiceWidget needs a list of pks, not object
instances.
data[f.name] = [obj.pk for obj in f.value_from_object
(instance)] """ HERE I get exception """

else:
data[f.name] = f.value_from_object(instance)



There is a hack I to overcome this I beleive...

Any help much appreciated, I am fighting with it for 4 days...
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: models.ManyToManyField: Multi-relationship in one table

2007-03-20 Thread hoamon

i persist in models.ManyToManyField so much so that i forgot this
method.

thank you Ivan.


--~--~-~--~~~---~--~~
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: models.ManyToManyField: Multi-relationship in one table

2007-03-20 Thread Ivan Sagalaev

hoamon wrote:
> Course|Trainee|Company
> C1|  T1   |  Com1
> C1|  T2   |  Com1
> C1|  T3   |  Com1
> C2|  T1   |  Com2
> C2|  T3   |  Com1
> C2|  T4   |  Com2
> C3|  T1   |  Com2

You really can't do it using standard ManyToManyField. The common way is 
to create this relation explicitly:

 class Record(models.Model): # or whatever name fits
   course = models.ForeignKey(Course)
   trainee = models.ForeignKey(Trainee)
   company = models.ForeignKey(Company)

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



models.ManyToManyField: Multi-relationship in one table

2007-03-19 Thread hoamon

i have no idea about keywords of this question, so i can't search the
answer of this question or the django document.

my question is
i have three models: Trainee, Company, Course.
and their relation table is below:

Course|Trainee|Company
C1|  T1   |  Com1
C1|  T2   |  Com1
C1|  T3   |  Com1
C2|  T1   |  Com2
C2|  T3   |  Com1
C2|  T4   |  Com2
C3|  T1   |  Com2

one course has many trainees, and one trainee mayby belongs to two
Companies.

for example:
T1 belongs to two companies, when T1 go to C1 course, he belongs to
Com1, but belongs to Com2 when he go to C2,C3.

how can i implement this relation in Couse Model, i cann't use the
syntax like

trainee = models.ManyToManyField(Trainee)
company = models.ManyToManyField(Company)

that makes two relation tables not one.

thank you for your patience.


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