ManyToManyField to Phones model for enter phones manually (not select from list)

33 views
Skip to first unread message

Anton Danilchenko

unread,
Sep 21, 2010, 10:14:13 AM9/21/10
to Django users
Hi all!

Please, help me undestand, how to solve next task.

I am show for user form with two fields: user name and text field,
where user input list of phones, separated by comma. User sholud enter
one or more phones separated by comma. If user enter not valid number
- we show error message like this "Phone 12345 is not valid. Please,
enter valid phone numbers separated by comma".

I have next two models:

class Phone(models.Model):
number = models.CharField(max_length=20)

class UserInfo(models.Model):
name = models.CharField(max_length=70)
phones = models.ManyToManyField(Phone)

User should enter numbers in CharField in format 0XX XXX XXXX. But, I
need save phone in database in format 0XXXXXXXXXX (only number without
spaces and other non-numbers digits).

Please, can you help me in this. I am new in Django, and I am read
more articles and not found solution. I think that I need create
custom fields for form and model.

P.S. Also, in admin section I should see CharField instead of
ManyToManyField.

nick...@gmail.com

unread,
Sep 21, 2010, 10:37:44 AM9/21/10
to django...@googlegroups.com
First I don't think you should be using the admin for such fine grained control of your data formats.
BUT if you do want to do that you would want to do your validation in custom admin forms. (created in your admin.py to do cleans on the data that's being submitted)

Second I wouldn't do a manytomany with this, I would instead create an inline on your userinfo model linking to phones. Then your Phone will have a FK relationship to your UserInfo.

in models.py
class Phone(model.Models):
    number = models.CharField(max_lenth=25)

class UserInfo(model.Models):
    name = models.CharField(max_length=50)

in admin.py

from myproject.myapp.models import Phone, User
class PhoneInline(models.TabularInline):
   model = Phone

class UserAdmin(models.ModelAdmin):
    inlines = [PhoneInline, ]

admin.site.register(User, UserAdmin)


What you get with that is the ability for a user to dynamically add as many phone numbers to a user as they want. Also you get the charfield (instead of the select box, which is what you get from the M2M field)

n



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.




--
Guadajuko! Vamos a correr!
 -"Cool! we are going to run!"

nick...@gmail.com

unread,
Sep 21, 2010, 10:38:55 AM9/21/10
to django...@googlegroups.com
Let me rephrase my first statement...You can use the admin for fine grained control of your data formats and characters. But don't use the Database to control the characters and formats. Use the database as a storage and make sure that anything you store gets cleaned.

n

Anton Danilchenko

unread,
Sep 21, 2010, 10:59:30 AM9/21/10
to Django users
In my case, I have Users with equal phone numbers. And I need only
ManyToMany relationship.

I need create custom field for get information from CharFiels,
validate this data, clean and save to datastore in separate model. If
this phone already exists - we get ID of this phone. And, if this is a
new phone number - we insert this phone to separate model and get new
ID.

nick...@gmail.com

unread,
Sep 21, 2010, 11:12:57 AM9/21/10
to django...@googlegroups.com
Maybe I don't understand your problem.

It sounds to me like your trying to make this way more difficult than it needs to be.

Also it sounds to me like your trying to assign the same phone number to multiple people. (which is fine) But doesn't make a lot of sense to me in a practical application.

n




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Anton Danilchenko

unread,
Sep 21, 2010, 1:15:30 PM9/21/10
to Django users
I describe my task more reallistical.

This is application for rent an apartment. User can create new message
with information about apartment, and also add one or more contact
phone numbers. One user, without registration, can create more that
one message on the site. For one message can add one phone number, for
other message add two phones.

I need to save only one copy of phone number in database in format
XXXXXXXX. For each phone I can set if this phone in black list or not
(model Phone have property "blocked" by default setted to False).

I want to save phone numbers in model Phone, and messages in model
Message. But, people can see input text field (CharField) where enter
one or more phone numbers separated by comma.

class Phone(models.Model):
number = models.CharField(max_length=20)
blocked = models.BooleanField(default=False)

class Message(models.Model):
# ... information about count of rooms, floor, cost, etc ...
phones = models.ManyToManyField(Phone)


Nick, thank you for you help. You are really very good people!


On 21 сен, 18:12, "nick.l...@gmail.com" <nick.l...@gmail.com> wrote:
> Maybe I don't understand your problem.
>
> It sounds to me like your trying to make this way more difficult than it
> needs to be.
>
> Also it sounds to me like your trying to assign the same phone number to
> multiple people. (which is fine) But doesn't make a lot of sense to me in a
> practical application.
>
> n
>
> On Tue, Sep 21, 2010 at 9:59 AM, Anton Danilchenko <
>
>
>
>
>
> anton.danilche...@gmail.com> wrote:
> > In my case, I have Users with equal phone numbers. And I need only
> > ManyToMany relationship.
>
> > I need create custom field for get information from CharFiels,
> > validate this data, clean and save to datastore in separate model. If
> > this phone already exists - we get ID of this phone. And, if this is a
> > new phone number - we insert this phone to separate model and get new
> > ID.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Django users" group.
> > To post to this group, send email to django...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > django-users...@googlegroups.com<django-users%2Bunsubscribe@google groups.com>
> > .

Anton Danilchenko

unread,
Sep 21, 2010, 1:37:12 PM9/21/10
to Django users
Also I need get list of all phones as with many-to-many relationship:

m = Message.objects.get(1)
for phone in m.phones:
ptint phone


On 21 сен, 20:15, Anton Danilchenko <anton.danilche...@gmail.com>
wrote:

nick...@gmail.com

unread,
Sep 21, 2010, 2:16:21 PM9/21/10
to django...@googlegroups.com
Personally....I don't like the idea of adding multiple phone numbers as comma separated values.  What happens when a user's trying to add more phone numbers than your charField's max_length?

So let me see if I understand what's going on here...

You have a problem where exists apartments for rent. People who want to rent these apartments will send messages to the owner of said apartment (through your website). This message will have associated to it, a User and Multiple Phone Numbers.

This message is then checked to see if the phone numbers associated to it have been black listed...
And then finally deliver the message?

n


To unsubscribe from this group, send email to django-users...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Anton Danilchenko

unread,
Sep 21, 2010, 2:50:06 PM9/21/10
to Django users
Message - this is a rental proposition (rooms, floor, cost, area,
country, cirty and contact phones).

Visitors can create new messages (message - this is a rent
proposition) and enter list of own phones (one or more phones). My
application store this phones not in char field, but save each phone
number to separate Phone morel - one phone in one model). If phone
already exists - message attached to this phone. If phone not exists -
we create new phone and attach message to this phone number.

In future, I tell to people - and see that this people not a good
man :-). I go to my application and mark this phone number to black
list. All future messages from this people to be marked as bad
messages.

Now I need to create own Form field called MultiPhoneField based on
CharField widget, but implenet logic of ManyToManyField. I need show
for user CharField where user enter comma separated phone numbers. I
check this numbers and if all numbers correct - store this numbers in
database in Phone model (each phone in separate record) and associate
message with list of this phones.

Can you please help me in this. I not understand how to create my own
Field based on ManyToManyField.

Thanks!

nick...@gmail.com

unread,
Sep 21, 2010, 4:21:32 PM9/21/10
to django...@googlegroups.com
read this here I think this'll explain what you're wanting.

To unsubscribe from this group, send email to django-users...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Anton Danilchenko

unread,
Sep 22, 2010, 2:39:54 PM9/22/10
to Django users
Thanks. I have read this article.

In my Form I have added validation for check phone numbers. All right
- I show user error message if given incorrect phone number. This is
simple.

I need to save this phone numbers in model Phone if all phone numbers
valid and all other fields in this form is valid. If one of the fields
in this form is incorrect I don't save any phone in Phone model.

As I see, ManyToManyField need get list of Phone IDs. But, actually I
have only full phone numbers in format 01234567. I need to get ID for
each phone (or create new record in table Phone). After this I need to
get list of Phone IDs and pass this data to ManyToManyField.

How can I do this? Thanks.

Anton Danilchenko

unread,
Sep 22, 2010, 3:42:58 PM9/22/10
to Django users
Now I have next solution (but still need your help).

1) I have added to my form custom field CharField for render field as
input <type="text">

2) I have created Form method clean_phones() for check all values is
valid

3) in views.py I have get field contents, split it by comma and delete
all non-numbers. In result I have list of phones in format 01234567

4) in views.py I create Phone model record for each phone
phones = ['01234567', '45678921']
ids = []
for phone in phones:
ids.append(Phone.objects.get_or_create(number=phone))

5) pass ids to real form field - ManyToManyField called phones


Please, help me understand how to pass all this logic in one place -
in custom field.

Anton Danilchenko

unread,
Sep 22, 2010, 4:30:21 PM9/22/10
to Django users
Ihave found solution.

I have changed only Form class:

1) added field for show text input field
phones = forms.CharField()

2) I have create in my Form instance method clear_phones() where I
check phones and save it all to database and get Phone model ids for
this phone numbers. And return this ids

All work well. Simple solution for this task. Thank you for help!

Steve Holden

unread,
Sep 22, 2010, 10:24:35 PM9/22/10
to django...@googlegroups.com
Most of the help seems to have been self-provided, so well done!

regards
Steve
--
DjangoCon US 2010 September 7-9 http://djangocon.us/

Reply all
Reply to author
Forward
0 new messages